- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create nested Python dictionary?
A Dictionary is a set of key: value pairs, with the requirement that the keys are unique (within one dictionary). Each key in a Dictionary is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces.
A nested Python Dictionary is a dictionary that has many Dictionaries. We can create a dictionary with that contain n number of dictionaries. With that, we can also nest n number of dictionaries already existing as dictionaries.
Let us first create a Python Dictionary and fetch all the values. Here, we have included 4 key-value pairs in the Dictionary and displayed them. Product, Model, Units, and Available are keys of the Dictionary. Except the Units key, all are having String values −
Example
# Creating a Dictionary myprod = { "Product":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" } # Displaying the Dictionary print(myprod)
Output
{'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
Now, we will create a Nested Dictionary with two Dictionaries similar to what we have shown above −
Create a Dictionary containing Two Dictionaries
We are adding two Dictionaries in a Dictionary making it a Nested Dictionary. Here, myprod dictionary will have two dictionaries Product1 and Product2 −
Here,
Example
# Creating a Nested Dictionary myprod = { "Product1" : { "Name":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" }, "Product2" : { "Name":"Laptop", "Model": "EEK", "Units": 90, "Available": "Yes" } } # Displaying the Nested Dictionary print("Nested Dictionary = \n",myprod)
Output
Nested Dictionary = {'Product1': {'Name': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}, 'Product2': {'Name': 'Laptop', 'Model': 'EEK', 'Units': 90, 'Available': 'Yes'}}
Create a Dictionary containing Three Dictionaries
We are adding three Dictionaries in a Dictionary making it a Nested Dictionary. Here, myprod dictionary will have three dictionaries Product1, Product2 and Product3 −
Example
# Creating a Nested Dictionary myprod = { "Product1" : { "Name":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" }, "Product2" : { "Name":"SmartTV", "Model": "LG", "Units": 70, "Available": "Yes" }, "Product3" : { "Name":"Laptop", "Model": "EEK", "Units": 90, "Available": "Yes" } } # Displaying the Nested Dictionary print("Nested Dictionary = \n",myprod)
Output
Nested Dictionary = {'Product1': {'Name': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}, 'Product2': {'Name': 'SmartTV', 'Model': 'LG', 'Units': 70, 'Available': 'Yes'}, 'Product3': {'Name': 'Laptop', 'Model': 'EEK', 'Units': 90, 'Available': 'Yes'}}
Nest Two Dictionaries that already exist as Dictionaries
We can also nest n number of dictionaries that already exist as Dictionaries. Therefore, we will first create two dictionaries and then add them into a separate dictionary making nested.
Example
# Creating a Nested Dictionary Product1 = { "Name":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" }, Product2 = { "Name":"Laptop", "Model": "EEK", "Units": 90, "Available": "Yes" } myprod = { "Product1" : Product1, "Product2" : Product2, } # Displaying the Nested Dictionary print("Nested Dictionary = \n",myprod)
Output
Nested Dictionary = {'Product1': ({'Name': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'},), 'Product2': {'Name': 'Laptop', 'Model': 'EEK', 'Units': 90, 'Available': 'Yes'}}
Above, we have two dictionaries Product1 and Product2 inside the myprod dictionary.
Nest Three Dictionaries that already exist as Dictionaries
We can also nest n number of dictionaries that already exist as Dictionaries. Therefore, we will first create three dictionaries and then add them into a separate dictionary making nested.
Example
# Creating a Nested Dictionary Product1 = { "Name":"Mobile", "Model": "XUT", "Units": 120, "Available": "Yes" }, Product2 = { "Name":"Laptop", "Model": "EEK", "Units": 90, "Available": "Yes" }, Product3 = { "Name":"SmartTV", "Model": "LG", "Units": 70, "Available": "Yes" } myprod = { "Product1" : Product1, "Product2" : Product2, "Product3" : Product3 } # Displaying the Nested Dictionary print("Nested Dictionary = \n",myprod)
Output
Nested Dictionary = {'Product1': ({'Name': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'},), 'Product2': ({'Name': 'Laptop', 'Model': 'EEK', 'Units': 90, 'Available': 'Yes'},), 'Product3': {'Name': 'SmartTV', 'Model': 'LG', 'Units': 70, 'Available': 'Yes'}}
Above, we have three dictionaries Product1, Product2, and Product3 inside the myprod dictionary.
- Related Articles
- How to sort a nested Python dictionary?
- How to recursively iterate a nested Python dictionary?
- Python - Convert flattened dictionary into nested dictionary
- Python Convert nested dictionary into flattened dictionary?
- How to count elements in a nested Python dictionary?
- Python Pandas - Convert Nested Dictionary to Multiindex Dataframe
- How to create a dictionary in Python?
- How to create Python dictionary from the value of another dictionary?
- Convert Nested Tuple to Custom Key Dictionary in Python
- How to access nested Python dictionary items via a list of keys?
- How to create Python dictionary from JSON input?
- How to create Python dictionary by enumerate function?
- How to create Python dictionary with duplicate keys?
- How to create an empty dictionary in Python?
- How to create a Python dictionary from text file?
