Python Nested Dictionary

    In this tutorial, we will discuss the concept of a Python nested dictionary i.e. a dictionary inside another dictionary. Also, we will learn how to create and access nested dictionaries and their elements in the Python programming language.

    With the help of some examples, we will also modify the nested dictionary elements. A normal dictionary is a collection of key-value pairs , and all the keys and their corresponding values are stored in an unordered fashion. We use curly braces { } to define a dictionary.

    Example:

    my_dict={"hello":"world","go":"pro", "ios":"apple"}

    What is a (Python) Nested Dictionary?

    When we define a dictionary inside a dictionary, it is known as a nested dictionary. Example:

    nest_dic = {"dict_1":{"hello":"world", "python":"programming"}, "dict_2": {"tech":"geekbuzz", 'OS':"window" } }

    Behind the code: In the above code, we have declared a variable nest_dic , which is a dictionary data type, but we will call it a nested dictionary because it has 2 dictionaries inside itself.

    Create a Python Nested Dictionary

    Let’s create 2 dictionaries inside a dictionary:

    mobiles = {
               1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} ,   
               2: {'Company' :'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000}
    }

    Behind the code: In the above code, our main dictionary is mobiles and it has two keys 1 & 2, and both the keys have their own dictionaries as values.

    Access Elements of a Nested Dictionary

    Like with a normal dictionary, we use square brackets for accessing a nested dictionary and the keys to get the corresponding values. In a nested dictionary, if we want to access the values then we have to use 2 square bracket pairs.

    Example:

     mobiles = 
    {
           1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} ,   
           2: {'Company':'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000}
    }
    print(mobiles [1] ['model'])
    print(mobiles [2] ['model'])
    print(mobiles [1] ['price'])
    print(mobiles [2] ['price'])
    
    Output:
    iphone
    Samsung
    67000
    87000

    Examples: Add Elements to the nested dictionary:

    mobiles = {
            1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} ,  
            2: {'Company':'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000}
    }
    #Adding a new dictionary in the mobiles dictionary
    mobiles[3] = {}
    mobiles[3]['Company'] = 'Nokia'
    mobiles[3]['model'] = 'A7'
    mobiles[3]['OS'] = 'Android'
    mobiles[3]['price'] = 17000
    print(mobiles[1])
    print(mobiles[2])
    print(mobiles[3])
    

    Output:

    {'Company': 'iphone', 'model': 'XR', 'OS': 'IOS', 'price': 67000}
    {'Company': 'Samsung', 'model': 'Note 10', 'OS': 'Android', 'price': 87000}
    {'Company': 'Nokia', 'model': 'A7', 'OS': 'Android', 'price': 17000}

    Behind the code: In the above example, we add a new dictionary in mobiles. F irst, we create a new dictionary at mobiles[3] then place values in that dictionary with the help of keys.

    There is another simple approach for adding a dictionary inside a dictionary.

    Let’s understand it with an example:

    mobiles = {
              1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} ,  
              2: {'Company':'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000}
    }
    #Adding new dictionary in the mobiles dictionary
    mobiles[3] = {'Company':'Nokia', 'model':'A7', 'OS':'Android', 'price':17000}
    print(mobiles[1])
    print(mobiles[2])
    print(mobiles[3])

    Delete Elements from a Python Nested Dictionary

    Python has a keyword del that we use to delete objects. So, in a dictionary, we can also delete items with the help of this del keyword.

    Example to delete an element from a nested dictionary:

    mobiles = {
               1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} ,  
               2: {'Company':'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000}
    }
    mobiles[3] = {'Company':'Nokia', 'model':'A7', 'OS':'Android', 'price':17000}
    del mobiles[1]['price']                    # Delete the price key and its value
    del mobiles[2]['price']
    del mobiles[3]['price']
    print(mobiles[1])
    print(mobiles[2])
    print(mobiles[3])

    Output:

    {'Company': 'iphone', 'model': 'XR', 'OS': 'IOS'}
    {'Company': 'Samsung', 'model': 'Note 10', 'OS': 'Android'}
    {'Company': 'Nokia', 'model': 'A7', 'OS': 'Android'}

    Behind the code: In the above example, we delete the price key and its values for each nested dictionary.

    Delete a Python Nested Dictionary

    As we have deleted the price element for each dictionary using the del keyword, we can also delete the nested dictionary using the same.

    Example:

    mobiles = {
             1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} ,  
             2: {'Company':'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000}
    }
    mobiles[3] = {'Company':'Nokia', 'model':'A7', 'OS':'Android', 'price':17000}
    del mobiles[3]
    print(mobiles)

    Output:

    {1: {'Company': 'iphone', 'model': 'XR', 'OS': 'IOS', 'price': 67000}, 2: {'Company': 'Samsung', 'model': 'Note 10', 'OS': 'Android', 'price': 87000}}

    Iterating Through a Nested Dictionary

    We can iterate over the items of nested dictionaries with the help of for loop.

    Example:

    mobiles = {
    
             1: {'Company':'iphone', 'model':'XR', 'OS':'IOS', "price":67000} ,  
             2: {'Company':'Samsung', 'model':'Note 10', "OS":'Android', 'price':87000},
             3: {'Company':'Nokia', 'model':'A7', 'OS':'Android', 'price':17000}
    }
    for i , nes_dict in mobiles.items():
        print(i, 'Product information')
        for j in nes_dict:
            print(j, nes_dict[j])
        print("************")

    Output:

    1 Product information
    Company iphone
    model XR
    OS IOS
    price 67000
    ************
    2 Product information
    Company Samsung
    model Note 10
    OS Android
    price 87000
    ************
    3 Product information
    Company Nokia
    model A7
    OS Android
    price 17000
    ************

    Points to Remember

    • A nested dictionary is a dictionary inside a dictionary.
    • Only the values of a dictionary can be a dictionary because dictionaries are mutable.
    • Once a dictionary is created, we can add and delete items from it.
    • We can access the nested dictionary using the parent dictionary key.

    Here we are listing the complete tutorials:

    Python Tutorials