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.


Advertisements