Python update nested dictionary | Example code

The dictionaries within a dictionary are called a Nested dictionary in Python. You can update the nested dictionary using the assignment operator or update method in Python.

Yes, updating the Nested dictionary is similar to updating a simple dictionary.

Example update nested dictionary in Python

Simple example code.

Appending nested list

It will add a new key-value into a dictionary.

Employee = {
    'emp1': {
        'name': 'John',
        'age': '29',
        'Designation': 'Programmer'
    },
    'emp2': {
        'name': 'Steve',
        'age': '45',
        'Designation': 'HR'
    }
}

Employee['name'] = 'Kate'

print(Employee)

Output:

{’emp1′: {‘name’: ‘John’, ‘age’: ’29’, ‘Designation’: ‘Programmer’}, ’emp2′: {‘name’: ‘Steve’, ‘age’: ’45’, ‘Designation’: ‘HR’}, ‘name’: ‘Kate’}

Updating existing key values in Nested dictionary.

This example updates the value for the mentioned key if it is present in the dictionary. Otherwise, it creates a new entry.

Employee = {
    'emp1': {
        'name': 'John',
        'age': '29',
        'Designation': 'Programmer'
    },
    'emp2': {
        'name': 'Steve',
        'age': '45',
        'Designation': 'HR'
    }
}
Employee['emp1']['name'] = 'Kate'
print(Employee)

Output:

Python update nested dictionary

Do comment if you have any doubts and suggestions on this Python dictionary topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.