Do you need to use dictionaries a lot in your day-to-day coding? If so, you will love Python it is one of the most popular programming languages for using dictionaries.
In this article, we will look at a Python Nested Dictionary and how to use it to your advantage when coding. We will also provide tips on ensuring your dictionaries are well-organized and easy to read. So whether you are a beginner or an experienced coder, this article is sure to be interesting.
Table of Contents
What is Nested Dictionary?
The nested dictionary is a collection of dictionaries in a single dictionary. To store the key-value pairs, we can use curly brackets in dictionaries. It is a data structure that allows easy lookup of values within a set. It consists of two parts, as normal dictionary, the inner dictionary and the outer dictionary.
Create Python Nested Dictionary
To create a Python nested dictionary, you must use the dict() function. This function takes an object or list as its parameter and creates a new dictionary based on that data. You can specify either keys or values for the key parameter, but you must include at least one key-value pair. The order of the keys in the nested dictionary will be randomizing, just like in regular dictionaries.
City = {1: {'Quaid e Azam Tomb', 'Mohatta Palace','Frere Hall'},
2: {'Badshahi Mosque','Minar e Pakistan' 'Lahore Museum'}}
print(City)
Output

Add Elements in Nested Dictionary
There are various ways to add items to a nested Dictionary. Nested_dict[dict][key] = ‘value’ is one technique to include a dictionary into the Nested dictionary. Another approach is to use Nested_dict[dict] = { ‘key’: ‘value} to incorporate the entire dictionary at once.
Lahore = {}
Lahore[1] = {}
Lahore[1]['Founder'] = 'Lahore fort'
Lahore[1]['name'] = 'Akbar the great'
Lahore[1]['born'] = '25 Oct'
print(Lahore[1])
Output

Access Element in Nested Dictionary
In order to access a dictionary items, you need to use the key method. This is simple: instead of using the item’s name, you will use the key given when it was created. For example, if your item has a key of “name”, then you would use “name”.
lahore = {'Founder': 'Lahore Fort', 'name': 'Akbar the great', 'born': '25 Oct'}
print(lahore)
Output

Delete Element in Nested Dictionary
The easiest way to delete an element in a nested dictionary is by using the dict() function. This will return a list of all the keys in the dictionary, and you can use the del or pop () function to remove the key from that list.
Lahore[1] = {}
Lahore[1]['Founder'] = 'Lahore fort'
Lahore[1]['name'] = 'Akbar the great'
Lahore[1]['born'] = '25 Oct'
del Lahore[1] ['name']
print(Lahore[1])
Output

Iterate Through in Nested Dictionary
There are a few ways to iterate through in a nested dictionary, but the easiest way is to use for-in loops. This allows you to loop through each key and value in turn.
City = {1: {'karachi': 'Quaid e Azam Tomb', 'Sea side': 'Mohatta Palace', 'Charna Island': 'Frere Hall'},
2: {'Lahore': 'Badshahi Mosque', 'Lahore Tomb': '', 'Minar e Pakistan': 'Lahore Museum'}}
for p_id, p_info in City.items():
print("\nCity ID:", p_id)
for key in p_info:
print(key + ':', p_info[key])
Output

Modifying Items in Nested Dictionaries
Modifying items in nested dictionaries can be helpful when you need to change the values of a particular word within a data set. For example, if you have an inventory database that stores each product’s quantity and unit price, you might want to change the unit price for a specific product.
people = {1: {'name': 'dustin', 'age': '14', 'sex': 'Male'},
2: {'name': 'steve', 'age': '20', 'sex': 'male'}}
people[2]['hero']='steven the great'
print(people)
Output

Merge Two Nested Dictionary
Merge the keys and values of one nested dictionary into another using the built-in update() method.
d1 = {
'A': {'x':1,'y':2,'z':3},
'B': {'x':4,'y':5,'z':6},
}
d2 = {
'C': {'x':7,'y':8,'z':9},
'B': {'x':10,'y':11,'z':12},
}
d1.update(d2)
print(d1)
Output

Also Learn, how to copy a dictionary in Python?
FAQs
How do you check if a value is in a nested dictionary Python?
Check if a Value Exists in a Dictionary; if the built-in function gets () and Key() does not return a Value, then it would return None.
Can we compare 2 dictionaries in Python?
The == operator will work for you. But, when you have specific demands, it gets more complicated. However, since Python does not provide a built-in function to compare two dictionaries and see if they are identical, we must use another language.
Conclusion
Python Nested dictionary can come in handy in many situations. They are a great way to manage data that resembles the hierarchical structure of relationship trees. The only thing is that you need to be careful while setting up your nested dictionary, as it can lead to errors if you do not handle it properly. We hope that this article helped you understand nested dictionaries in Python better. Keep reading.