ADVERTISEMENT
ADVERTISEMENT

Python program for accessing elements from a nested dictionary

Here, we are going to learn how to access elements from a nested dictionary? Also, writing a Python program to access elements from a nested dictionary.
Submitted by Shivang Yadav, on March 21, 2021

There are some of the methods to access elements from a nested dictionary,

  1. By using key name
  2. By using get() method

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.

Syntax:

dictionary_name.get(dictionary_name_as_key).get(key_of_the_inside_dictionary)

Program:

# 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'))

Output:

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

Python Dictionary Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.