belongs to collection: Python Dictionary Programs
There are some of the methods to access elements from a nested dictionary,
1) Access elements by using key name
We can access the elements of a nested dictionary by passing the key as the name of the dictionary defined inside the dictionary and the key name of the nested dictionary.
Syntax:
dictionary_name[dictionary_name_as_key][key_of_the_inside_dictionary]
Program:
# Python program for accessing elements # from a nested dictionary using key name # Dictionary Record = {'personal' :{'id': 101, 'name': 'Amit', 'age' : 23}, 'exam' :{'total': 550, 'perc': 91.6, 'grade' : 'A'} } # printing Dictionary print("Record...") print(Record) # Printing the both dictionaries print("personal...") print(Record['personal']) print("exam...") print(Record['exam']) # printing elements of nested dictionaries print("Dictionary \'personal\' elements...") print(Record['personal']['id']) print(Record['personal']['name']) print(Record['personal']['age']) print("Dictionary \'exam\' elements...") print(Record['exam']['total']) print(Record['exam']['perc']) print(Record['exam']['grade'])
Output:
Record... {'personal': {'name': 'Amit', 'id': 101, 'age': 23}, 'exam': {'perc': 91.6, 'total': 550, 'grade': 'A'}} personal... {'name': 'Amit', 'id': 101, 'age': 23} exam... {'perc': 91.6, 'total': 550, 'grade': 'A'} Dictionary 'personal' elements... 101 Amit 23 Dictionary 'exam' elements... 550 91.6 A
2) Access elements by using get() method
The get() method can be used to access an item of the nested dictionary.
dictionary_name.get(dictionary_name_as_key).get(key_of_the_inside_dictionary)
# Python program for accessing elements # from a nested dictionary using get() method # Dictionary Record = {'personal' :{'id': 101, 'name': 'Amit', 'age' : 23}, 'exam' :{'total': 550, 'perc': 91.6, 'grade' : 'A'} } # printing Dictionary print("Record...") print(Record) # Printing the both dictionaries print("personal...") print(Record.get('personal')) print("exam...") print(Record.get('exam')) # printing elements of nested dictionaries print("Dictionary \'personal\' elements...") print(Record.get('personal').get('id')) print(Record.get('personal').get('name')) print(Record.get('personal').get('age')) print("Dictionary \'exam\' elements...") print(Record.get('exam').get('total')) print(Record.get('exam').get('perc')) print(Record.get('exam').get('grade'))
Record... {'personal': {'age': 23, 'name': 'Amit', 'id': 101}, 'exam': {'total': 550, 'grade': 'A', 'perc': 91.6}} personal... {'age': 23, 'name': 'Amit', 'id': 101} exam... {'total': 550, 'grade': 'A', 'perc': 91.6} Dictionary 'personal' elements... 101 Amit 23 Dictionary 'exam' elements... 550 91.6 A
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
1) Access elements by using key name
We can access the elements of a nested dictionary by passing the key as the name of the dictionary defined inside the dictionary and the key name of the nested dictionary.
Syntax:
Program:
Output:
2) Access elements by using get() method
The get() method can be used to access an item of the nested dictionary.
Syntax:
Program:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer