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. By Shivang Yadav Last updated : September 18, 2023

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

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

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

The output of the above program is:

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

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

The output of the above program is:

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 »


Related Programs

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.