Python provides a dictionary in order to store key-value pairs. A nested dictionary is a dictionary where some values are dictionary too. By using nested dictionaries complex data structures can be defined easily in a key/value pair. In this tutorial, we also examine how to access, update, and edit nested directories.
Create Nested Dictionary
A nested dictionary means a dictionary inside a dictionary. This can be also called a parent and child dictionary there the values of the parent dictionary are child dictionaries. In the following example, we will create families where multiple families will be stored inside nested dictionaries. The syntax of the nested dictionary is like below this can be extended with more levels and children too.
mydict = {"parentkey1":{"childkey1":"childvalue1"},
"parentkey2":{"childkey2":"childvalue2"},
"parentkey3":{"childkey3":"childvalue3"}}
Now let’s create a real nested dictionary that contains students in a nested way.
mydict = {1:{"name":"ismail","number":234},
2:{"name":"ahmet","number":321},
3:{"name":"elif","number":765}
}
If this dictionary is formatted properly it will look like the below. This formatting is easier to understand in parent and child dictionaries.
mydict = {
"parentkey1":{
"childkey1":"childvalue1"
},
"parentkey2":{
"childkey2":"childvalue2"
},
"parentkey3":{
"childkey3":"childvalue3"
}
}
Access Nested Dictionary Elements
Nested elements can be accessed by using their keys. The parent keys like 1, 2, and 3 stores a dictionary as a value so we will first use the parent keys and then specify the child key in order to get items or elements.
mydict = {1:{"name":"ismail","number":234},
2:{"name":"ahmet","number":321},
3:{"name":"elif","number":765}
}
#Print the key 2 name value
print(mydict[2][name])
#Print the key 3 name number
print(mydict[3][number])
#Print the key 1 name and number values
print(mydict[1])
Add Nested Dictionary
New items can be added to the existing nested dictionary easily. New items will be a new dictionary or a single item or key-value pair. It is similar to the access of the nested elements where we will provide new items.
mydict = {1:{"name":"ismail","number":234},
2:{"name":"ahmet","number":321},
3:{"name":"elif","number":765}
}
#We will add new item with key 4 and a dictionary
mydict[4]= {"name":"ahmet","number":321}
#We will add new key/value pair "surname":"baydan" for the first item
mydict[1]= {"name":"ismail","number":234,"surname":"baydan"}
Change or Update Nested Dictionary
Already existing nested dictionary values can be updated easily by providing keys we want to update. In the following example, we update the nested dictionary named mydict
which parent key is 4
and child key
is name
.
mydict = {1:{"name":"ismail","number":234},
2:{"name":"ahmet","number":321},
3:{"name":"elif","number":765}
}
#We will add new item with key 4 and a dictionary
mydict[4]["name"] = "ahmet ali"
#We will add new key/value pair "surname":"baydan" for the first item
mydict[1]= {"name":"ismail","number":234,"surname":"baydan"}
Remove/Delete Items in Nested Dictionary
We can delete a single item or a child dictionary from a nested dictionary. First, we will delete a single element from the child dictionary of a nested dictionary. We will use the del statement provided by Python to delete the specified object.
mydict = {1:{"name":"ismail","number":234},
2:{"name":"ahmet","number":321},
3:{"name":"elif","number":765}
}
# Delete the "number" item
del mydict[1]["number"]
Delete Child Dictionary From Nested Parent Dictionary
Alternatively, we can remove or delete a child dictionary from a nested dictionary like the one below. We will again use the del statement to remove a specific dictionary.
mydict = {1:{"name":"ismail","number":234},
2:{"name":"ahmet","number":321},
3:{"name":"elif","number":765}
}
# Delete the child dictionary with key 2
del mydict[2]
Iterate over Nested Dictionary
The nested dictionary contains parent and child dictionaries where it has multi-level hierarchies. As dictionaries are iterable or enumerable data types a nested dictionary is also enumerable easily. In the following example, we have a nested dictionary that is two-level and we will use two for loop in order to iterate over all elements of both parent and child dictionaries.
mydict = {1:{"name":"ismail","number":234},
2:{"name":"ahmet","number":321},
3:{"name":"elif","number":765}
}
for key , value in mydict.items():
for name , number in value:
print("Parent Key "+key+" Child name "+name+" Child number "+number)