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
# Creating dictionary which contains lists 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
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.