Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – How to Iterate over nested dictionary ?

  • Last Updated : 10 Oct, 2021

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. 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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(['Bobby', 1, 20])
dict_values(['ojaswi', 2, 22])
dict_values(['rohith', 3, 20])

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!