Python Nested Dictionary Tutorial

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.

Create Nested Dictionary

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 lets create real nested dictionary which contains students in a nested way.

mydict = {1:{"name":"ismail","number":234},
          2:{"name":"ahmet","number":321},
          3:{"name":"elif","number":765}
}

Access Nested Dictonary Elements

Nested elements can be accessed by using their keys. The parent keys like 1, 2, 3 store 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 into 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

Allready existing nested dictionary values can be updated easily by providing keys we want to update.

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"]

Alternatively, we can remove or delete a child dictionary from a nested dictionary-like 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 which 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)

Leave a Comment