Skip to content
Related Articles

Related Articles

Python – How to Iterate over nested dictionary ?

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 05 Aug, 2022
View Discussion
Improve Article
Save Article

In this article, we will discuss how to iterate over a nested dictionary in Python.

Nested dictionary means dictionary inside a dictionary and we are going to see every possible way of iterating over such a data structure. 

Nested dictionary in use:

{‘Student 1’: {‘Name’: ‘Bobby’, ‘Id’: 1, ‘Age’: 20}, 

‘Student 2’: {‘Name’: ‘ojaswi’, ‘Id’: 2, ‘Age’: 22}, 

‘Student 3’: {‘Name’: ‘rohith’, ‘Id’: 3, ‘Age’: 20}}

For this we will use for loop to iterate through a dictionary to get the all the key , values of nested dictionaries.

Syntax:

for i in dictionary_name:
     print(dictionary_name[i])

where

  • dictionary_name is the input dictionary
  • dictionary_name[i] is the value to get dictionaries

Example: Python program to get the nested dictionaries from a dictionary

Python3




# create a nested dictionary with 3
# fields of 3 students
data = {
    'Student 1': {'Name': 'Bobby', 'Id': 1, "Age": 20},
    'Student 2': {'Name': 'ojaswi', 'Id': 2, "Age": 22},
    'Student 3': {'Name': 'rohith', 'Id': 3, "Age": 20},
}
 
 
# iterate all the nested dictionaries with
# both keys and values
for i in data:
    
    # display
    print(data[i])


Output:

{'Name': 'Bobby', 'Id': 1, 'Age': 20}
{'Name': 'ojaswi', 'Id': 2, 'Age': 22}
{'Name': 'rohith', 'Id': 3, 'Age': 20}

It is also possible to get only either keys or values if the that is what the requirement asks for. Again for this for loop is employed with a little variation.

To get keys from the nested dictionary for each iteration keys() function is called.

Syntax:

data[i].keys()

Example: Python program to get keys from the nested dictionary

Python3




# create a nested dictionary with 3 fields of 3 students
data = {
    'Student 1': {'Name': 'Bobby', 'Id': 1, "Age": 20},
    'Student 2': {'Name': 'ojaswi', 'Id': 2, "Age": 22},
    'Student 3': {'Name': 'rohith', 'Id': 3, "Age": 20},
}
 
 
# iterate all the nested dictionaries with keys
for i in data:
  # display
    print(data[i].keys())


Output:

dict_values(['Name', Id, Age])
dict_values(['Name', Id, Age])
dict_values(['Name', Id, Age])

Similarly to get values, after each iteration values() function is used to get the job done.

Syntax:

data[i].values()

Example: Python program to get values from the nested dictionary

Python3




# create a nested dictionary with 3 fields of 3 students
data = {
    'Student 1': {'Name': 'Bobby', 'Id': 1, "Age": 20},
    'Student 2': {'Name': 'ojaswi', 'Id': 2, "Age": 22},
    'Student 3': {'Name': 'rohith', 'Id': 3, "Age": 20},
}
 
 
# iterate all the nested dictionaries with values
for i in data:
  # display
    print(data[i].values())


Output:

dict_values(['Bobby', 1, 20])
dict_values(['ojaswi', 2, 22])
dict_values(['rohith', 3, 20])

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!