Python – How to Iterate over nested dictionary ?

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. 
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 studentsdata = {    '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 valuesfor 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 studentsdata = {    '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 keysfor 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 studentsdata = {    '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 valuesfor i in data:  # display    print(data[i].values())
Output:
dict_values(['Bobby', 1, 20])
dict_values(['ojaswi', 2, 22])
dict_values(['rohith', 3, 20])
Picked
Python dictionary-programs
python-dict
Python
Python Programs
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python | os.path.join() method
Python OOPs Concepts
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary
Python Program for Fibonacci numbers