Q:

Python program for accessing elements from a nested dictionary

belongs to collection: Python Dictionary Programs

0

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

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Dictionary Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program for removing elements from a dictio... >>
<< Python program for accessing elements from a dicti...