Python – Accessing Items in Lists Within Dictionary
Given a dictionary with values as a list, the task is to write a python program that can access list value items within this dictionary.
Method 1: Manually accessing the items in the list
This is a straightforward method, where the key from which the values have to be extracted is passed along with the index for a specific value.
Syntax:
dictionary_name[key][index]
Example: direct indexing
Python3
country = {
"India" : [ "Delhi" , "Maharashtra" , "Haryana" ,
"Uttar Pradesh" , "Himachal Pradesh" ],
"Japan" : [ "Hokkaido" , "Chubu" , "Tohoku" , "Shikoku" ],
"United States" : [ "New York" , "Texas" , "Indiana" ,
"New Jersey" , "Hawaii" , "Alaska" ]
}
print (country[ "India" ])
print (country[ "India" ][ 0 ])
print (country[ "India" ][ 1 ])
print (country[ "United States" ][ 3 ])
print (country[ 'Japan' ][ 2 ])
|
Output :
[‘Delhi’, ‘Maharashtra’, ‘Haryana’, ‘Uttar Pradesh’, ‘Himachal Pradesh’]
Delhi
Maharashtra
New Jersey
Tohoku
Method 2: Using Loop
The easiest way to achieve the task given is to iterate over the dictionary.
Example: Using loop
Python3
country = {
"India" : [ "Delhi" , "Maharashtra" , "Haryana" ,
"Uttar Pradesh" , "Himachal Pradesh" ],
"Japan" : [ "Hokkaido" , "Chubu" , "Tohoku" , "Shikoku" ],
"United States" : [ "New York" , "Texas" , "Indiana" ,
"New Jersey" , "Hawaii" , "Alaska" ]
}
for key, val in country.items():
for i in val:
print ( "{} : {}" . format (key, i))
print ( "--------------------" )
|
Output :
India : Delhi
India : Maharashtra
India : Haryana
India : Uttar Pradesh
India : Himachal Pradesh
——————–
Japan : Hokkaido
Japan : Chubu
Japan : Tohoku
Japan : Shikoku
——————–
United States : New York
United States : Texas
United States : Indiana
United States : New Jersey
United States : Hawaii
United States : Alaska
——————–
Time Complexity: O(n*n), where n is the values in dictionary
Auxiliary Space: O(1), extra constant space required
Method 3: Accessing a particular list of the key
This is more or less the first two methods combined, where using the key the value list is iterated.
Example: Accessing a particular list of the key
Python3
country = {
"India" : [ "Delhi" , "Maharashtra" , "Haryana" ,
"Uttar Pradesh" , "Himachal Pradesh" ],
"Japan" : [ "Hokkaido" , "Chubu" , "Tohoku" , "Shikoku" ],
"United States" : [ "New York" , "Texas" , "Indiana" ,
"New Jersey" , "Hawaii" , "Alaska" ]
}
for i in country[ 'Japan' ]:
print (i)
for i in country[ 'India' ]:
print (i)
for i in country[ 'United States' ]:
print (i)
|
Output:
Hokkaido
Chubu
Tohoku
Shikoku
Delhi
Maharashtra
Haryana
Uttar Pradesh
Himachal Pradesh
New York
Texas
Indiana
New Jersey
Hawaii
Alaska
Method 4: Using list slicing
This is a modified version of the first method, here instead of index for the value list, we pass the slicing range.
Syntax:
dictionary_name[key][start_index : end_index]
Example: using list slicing
Python3
country = {
"India" : [ "Delhi" , "Maharashtra" , "Haryana" ,
"Uttar Pradesh" , "Himachal Pradesh" ],
"Japan" : [ "Hokkaido" , "Chubu" , "Tohoku" , "Shikoku" ],
"United States" : [ "New York" , "Texas" , "Indiana" ,
"New Jersey" , "Hawaii" , "Alaska" ]
}
print (country[ "India" ][: 3 ])
print (country[ "Japan" ][ - 2 :])
print (country[ "India" ][: - 3 ])
print (country[ "United States" ][ 1 : 5 ])
|
Output :
[‘Delhi’, ‘Maharashtra’, ‘Haryana’]
[‘Tohoku’, ‘Shikoku’]
[‘Delhi’, ‘Maharashtra’]
[‘Texas’, ‘Indiana’, ‘New Jersey’, ‘Hawaii’]
Method 5 : Using for loop and f-string
In this approach, we will use the for loop with the f-strings to fetch the values from the dictionary. It is a little modification of the method where the normal for loop has been used.
Step – 1 : Firstly we will iterate over the dictionary using two variables, one to point at the key and one to the values. We will also use the .items() method inside the for loop.
Step – 2: Next, as the dictionary consists of values which are of type lists so there must be more than one item in the list which we have to fetch and print. This is why we will again iterate over the second variable we took earlier to fetch each element of that particular key and print it.
Step – 3 : After printing all the elements of a certain key we will print something to separate it with the others like a line of dash(-) or dots(.).
Python3
country = {
"India" : [ "Delhi" , "Maharashtra" , "Haryana" ,
"Uttar Pradesh" , "Himachal Pradesh" ],
"Japan" : [ "Hokkaido" , "Chubu" , "Tohoku" , "Shikoku" ],
"United States" : [ "New York" , "Texas" , "Indiana" ,
"New Jersey" , "Hawaii" , "Alaska" ]
}
for key, val in country.items():
for i in val:
print (f "{key} : {i}" )
print ( "--------------------" )
|
Output
India : Delhi
India : Maharashtra
India : Haryana
India : Uttar Pradesh
India : Himachal Pradesh
--------------------
Japan : Hokkaido
Japan : Chubu
Japan : Tohoku
Japan : Shikoku
--------------------
United States : New York
United States : Texas
United States : Indiana
United States : New Jersey
United States : Hawaii
United States : Alaska
--------------------
Time Complexity – O(n**2) # using two for loops
Space Complexity – O(1) # No extra space has been used.
Last Updated :
27 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...