Python Dictionary 7

Access elements of a Nested Dictionary

To access an element of a nested dictionary, we use indexing [] syntax in Python.

Example : Access the elements using the [] syntax

employee = {1: {‘name’: ‘Amit’, ‘age’: ’27’, ‘sex’: ‘Male’},
2: {‘name’: ‘Soniya’, ‘age’: ’22’, ‘sex’: ‘Female’}}

print(employee[1][‘name’])
print(employee[1][‘age’])
print(employee[1][‘sex’])

When we run above program, it will output:
Amit
27
Male

In the above program, we print the value of key name using i.e. people[1][‘name’] from internal dictionary 1. Similarly, we print the value of age and sex one by one.

#employee details
employee = {1: {'name': 'Amit', 'age': '27', 'sex': 'Male'},
          2: {'name': 'Soniya', 'age': '22', 'sex': 'Female'}}

print(employee[1]['name'])
print(employee[1]['age'])
print(employee[1]['sex'])

Output:

Amit
27
Male
>>> 

Visitor Counter

0056731