Python – Accessing Items in Lists Within Dictionary

23 Aug, 2021
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. 
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
#  Creating dictionary which contains listscountry = {    "India": ["Delhi", "Maharastra", "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’, ‘Maharastra’, ‘Haryana’, ‘Uttar Pradesh’, ‘Himachal Pradesh’]
Delhi
Maharastra
New Jersey
Tohoku
The easiest way to achieve the task given is to iterate over the dictionary.
Example: Using loop
Python3
#  Creating dictionary which contains listscountry = {    "India": ["Delhi", "Maharastra", "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 : Maharastra
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
——————–
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
#  Creating dictionary which contains listscountry = {    "India": ["Delhi", "Maharastra", "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
Maharastra
Haryana
Uttar Pradesh
Himachal Pradesh
New York
Texas
Indiana
New Jersey
Hawaii
Alaska
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
#  Creating dictionary which contains listscountry = {    "India": ["Delhi", "Maharastra", "Haryana",              "Uttar Pradesh", "Himachal Pradesh"],    "Japan": ["Hokkaido", "Chubu", "Tohoku", "Shikoku"],    "United States": ["New York", "Texas", "Indiana",                      "New Jersey", "Hawaii", "Alaska"]}  # extract the first 3 cities of Indiaprint(country["India"][:3])  # extract last 2 cities from Japanprint(country["Japan"][-2:])  # extract all cities except last 3 cities from indiaprint(country["India"][:-3])  # extract 2th to 5th cities from usprint(country["United States"][1:5])
Output :
[‘Delhi’, ‘Maharastra’, ‘Haryana’]
[‘Tohoku’, ‘Shikoku’]
[‘Delhi’, ‘Maharastra’]
[‘Texas’, ‘Indiana’, ‘New Jersey’, ‘Hawaii’]
Picked
Python dictionary-programs
python-dict
Python
Python Programs
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary
Python Program for Fibonacci numbers