
In this tutorial we will discuss nested dictionaries in python, we will learn how can we create and excess nested dictionaries and its elements. With the help of some examples, we will also modify the nested dictionary elements.
A normal dictionary is a collection of key: value pairs, and all the keys and their corresponding values items store unordered. To define a dictionary we use curly braces { }.
Example:
my_dict={“hello”:”world”,”go”:”pro”, “ios”:”apple”}
What is a Nested Dictionary?
When we define a dictionary inside a dictionary is known as Nested dictionary, we can also say a nested dictionary means a dictionary inside a dictionary.
Example:
nest_dic = {“dict_1”:{“hello”:”world”, “python”:”programming”}, “dict_2”: {“tech”:”geekbuzz”, ‘OS’:”window” } }
Behind the code:
In the above code, we have declared a variable nest_dic which is a dictionary by data type, but we will call it nested dictionary because it has dictions inside itself.
Create a Nested Dictionary:
Let’s Create 3 dictionaries inside a dictionary:
mobiles = { 1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} , 2: {'Company' :'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000} }
Behind the code:
In the above code, our main dictionary is mobiles and it has two keys 1 & 2, and both the keys have their own dictionaries as a value.
Access Elements of a Nested dictionary:
Like a normal dictionary, to access a Nested dictionary we use sq. brackets and the keys to get the corresponding values. In a nested dictionary, if we want to access the nested dictionary, values we have to use 2 Sq. brackets pairs.
Example:
mobiles = { 1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} , 2: {'Company':'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000} } print(mobiles [1] ['model']) print(mobiles [2] ['model']) print(mobiles [1] ['price']) print(mobiles [2] ['price']) Output:
iphone Samsung 67000 87000
Examples:
Add Elements to the nested dictionary:
mobiles = { 1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} , 2: {'Company':'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000} } #Adding a new dictionary in the mobiles dictionary mobiles[3] = {} mobiles[3]['Company'] = 'Nokia' mobiles[3]['model'] = 'A7' mobiles[3]['OS'] = 'Android' mobiles[3]['price'] = 17000 print(mobiles[1]) print(mobiles[2]) print(mobiles[3])
Output:
{'Company': 'iphone', 'model': 'XR', 'OS': 'IOS', 'price': 67000} {'Company': 'Samsung', 'model': 'Note 10', 'OS': 'Android', 'price': 87000} {'Company': 'Nokia', 'model': 'A7', 'OS': 'Android', 'price': 17000}
Behind the code:
In the above example, we add a new dictionary in mobiles, first, we create a new dictionary at mobiles[3] then place values in that dictionaries with the help of keys.
There is another simple approach to add a dictionary inside a dictionary let’s understand it with an example:
mobiles = { 1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} , 2: {'Company':'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000} } #Adding new dictionary in the mobiles dictionary mobiles[3] = {'Company':'Nokia', 'model':'A7', 'OS':'Android', 'price':17000} print(mobiles[1]) print(mobiles[2]) print(mobiles[3])
Delete elements from a Dictionary.
Python has a keyword del which is used to delete objects, so in a dictionary, we can also delete its items with the help of this del keyword.
Example to delete an element from a nested dictionary;
mobiles = { 1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} , 2: {'Company':'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000} } mobiles[3] = {'Company':'Nokia', 'model':'A7', 'OS':'Android', 'price':17000} del mobiles[1]['price'] # Delete the price key and its value del mobiles[2]['price'] del mobiles[3]['price'] print(mobiles[1]) print(mobiles[2]) print(mobiles[3])
Output:
{'Company': 'iphone', 'model': 'XR', 'OS': 'IOS'} {'Company': 'Samsung', 'model': 'Note 10', 'OS': 'Android'} {'Company': 'Nokia', 'model': 'A7', 'OS': 'Android'}
Behind the code:
In the above example, we delete the price key and its values for each nested dictionary.
Delete a nested dictionary.
As we have deleted the price element for each dictionary using del keyword, we can also delete the nested dictionary using del keyword.
Example:
mobiles = { 1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} , 2: {'Company':'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000} } mobiles[3] = {'Company':'Nokia', 'model':'A7', 'OS':'Android', 'price':17000} del mobiles[3] print(mobiles)
Output:
{1: {'Company': 'iphone', 'model': 'XR', 'OS': 'IOS', 'price': 67000}, 2: {'Company': 'Samsung', 'model': 'Note 10', 'OS': 'Android', 'price': 87000}}
Iterating Through a Nested Dictionary:
With the help of for loop, we can iterate over the items of nested dictionaries.
Example: mobiles = { 1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} , 2: {'Company':'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000}, 3: {'Company':'Nokia', 'model':'A7', 'OS':'Android', 'price':17000} } for i , nes_dict in mobiles.items(): print(i, 'Product information') for j in nes_dict: print(j, nes_dict[j]) print("************")
Output:
1 Product information Company iphone model XR OS IOS price 67000 ************ 2 Product information Company Samsung model Note 10 OS Android price 87000 ************ 3 Product information Company Nokia model A7 OS Android price 17000 ************
Points to Remember:
- A dictionary inside a dictionary known as a nested dictionary.
- Only the values of a dictionary can be a dictionary because dictionaries are mutable.
- Once a dictionary is created, we can add and delete items from it.
- We can access the nested dictionary using the parent dictionary key.
Here we are listing the complete tutorials:
Python Tutorials
Introduction
- Getting Started
- Keywords and Identifiers
- Statements & Comments
- Python Variables
- Python Datatypes
- Python Type Conversion
- Python I/O and import
- Python Operators
- Python Namespace
Python Flow Control
Python Functions
Python Datatypes
Python Files
Python Object & Class
Python Advanced Topics
- Python Iterator
- Python Generator
- Python Closure
- Python Decorators
- Python Property
- Python RegEx
- Python Examples