Python - How to Iterate over nested dictionary ?
Last Updated :
28 Feb, 2023
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}
Time complexity : O(n)
Space complexity : O(n)
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])
Time complexity : O(n)
Space complexity : O(n)
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])
Time complexity : O(n)
Space complexity : O(n)
Similar Reads
Merging or Concatenating two Dictionaries in Python Combining two dictionaries is a common task when working with Python, especially when we need to consolidate data from multiple sources or update existing records. For example, we may have one dictionary containing user information and another with additional details and we'd like to merge them into
2 min read
How to Compare Two Dictionaries in Python In this article, we will discuss how to compare two dictionaries in Python. The simplest way to compare two dictionaries for equality is by using the == operator.Using == operatorThis operator checks if both dictionaries have the same keys and values.Pythond1 = {'a': 1, 'b': 2} d2 = {'a': 1, 'b': 2}
2 min read
Python Dictionary Comprehension Like List Comprehension, Python allows dictionary comprehensions. We can create dictionaries using simple expressions. A dictionary comprehension takes the form {key: value for (key, value) in iterable}Python Dictionary Comprehension ExampleHere we have two lists named keys and value and we are iter
4 min read
How to Add Values to Dictionary in Python The task of adding values to a dictionary in Python involves inserting new key-value pairs or modifying existing ones. A dictionary stores data in key-value pairs, where each key must be unique. Adding values allows us to expand or update the dictionary's contents, enabling dynamic manipulation of d
3 min read
Add new keys to a dictionary in Python In this article, we will explore various methods to add new keys to a dictionary in Python. Let's explore them with examples:Using Assignment Operator (=)The simplest way to add a new key is by using assignment operator (=).Pythond = {"a": 1, "b": 2} d["c"] = 3 print(d)Output{'a': 1, 'b': 2, 'c': 3}
2 min read
Add Item after Given Key in Dictionary - Python The task of adding an item after a specific key in a Pythondictionary involves modifying the order of the dictionary's key-value pairs. Since Python dictionaries maintain the insertion order, we can achieve this by carefully placing the new key-value pair after the target key. For example, consider
4 min read
Python - Ways to remove a key from dictionary We are given a dictionary and our task is to remove a specific key from it. For example, if we have the dictionary d = {"a": 1, "b": 2, "c": 3}, then after removing the key "b", the output will be {'a': 1, 'c': 3}.Using pop()pop() method removes a specific key from the dictionary and returns its cor
3 min read
Removing Dictionary from List of Dictionaries - Python We are given a list of dictionaries, and our task is to remove specific dictionaries based on a condition. For instance given the list: a = [{'x': 10, 'y': 20}, {'x': 30, 'y': 40}, {'x': 50, 'y': 60}], we might want to remove the dictionary where 'x' equals 30 then the output will be [{'x': 10, 'y':
3 min read
Python - Remove item from dictionary when key is unknown We are given a dictionary we need to remove the item or the value of key which is unknown. For example, we are given a dictionary a = {'a': 10, 'b': 20, 'c': 30} we need to remove the key 'b' so that the output dictionary becomes like {'a': 10, 'c': 30} . To do this we can use various method and app
4 min read
Python - Test if all Values are Same in Dictionary Given a dictionary, test if all its values are the same. Input : test_dict = {"Gfg" : 8, "is" : 8, "Best" : 8} Output : True Explanation : All element values are same, 8. Input : test_dict = {"Gfg" : 8, "is" : 8, "Best" : 9} Output : False Explanation : All element values not same. Method #1: Using
7 min read