The dict.update() method updates a dictionary with a (key-value) pair element from another dictionary, or from an iterable of (key-value) pair elements.
Syntax
dict.update([dictionary/iterable])Parameters
- The dict.update() method inputs either an iterable object of (key-value) pair elements (tuples in most cases), or another dictionary.
- Also, if the Python dict.update() method is applied to a dictionary without any parameters passed, then no changes to the dictionary occur, so the dictionary remains the same.
💡 Side note: The dict.update() method inserts a specified (key-value) pair element into a dictionary if the key does not exist.
Return Value
- The Python dict.update() method performs it’s update operation, but does not return any value (returns a identities = {'id_1': 'jim', 'id_2': 'tammy', 'id_3': 'sarah', 'id_4': 'bob'} change_id_3 = {'id_3': 'amada'} identities.update(change_id_3) print(identities)3 value).
Basic Example
Example using the Python dict.update() method to update the value of a key in a dictionary:
identities = {'id_1': 'jim', 'id_2': 'tammy', 'id_3': 'sarah', 'id_4': 'bob'} change_id_3 = {'id_3': 'amada'} identities.update(change_id_3) print(identities)Output:
{'id_1': 'jim', 'id_2': 'tammy', 'id_3': 'amada', 'id_4': 'bob'}This example shows how to update a value of a particular key in a dictionary by passing another dictionary with the key and its changed value as a parameter to the dict.update() method.
Add Key Value Pair to Python Dictionary
The following example shows how to add (key-value) pair elements to a Python dictionary using the dict.update() method:
groceries = {} apples = {'apples': 5} oranges = {'oranges': 6} peaches = {'peaches': 5} groceries.update(apples) groceries.update(oranges) groceries.update(peaches) print(groceries) # {'apples': 5, 'oranges': 6, 'peaches': 5}This example shows how to individually insert (key-value) pair elements into a dictionary.
Passing a Tuple to dict.update()
An example on how to pass a tuple to a Python dictionary using the dict.update() method:
store_items = {} store_items.update(pens = 3, notebooks = 4, desks = 4, shelves = 6) print(store_items) # {'pens': 3, 'notebooks': 4, 'desks': 4, 'shelves': 6}In the previous example, applying the dict.update() method to a dictionary with one (key-value) pair element is good when only one (key-value) pair element needs to be inserted into a dictionary,
But this operation becomes tedious if multiple (key-value) pair elements are required to be inserted into a dictionary. This example of passing a tuple to the Python dict.update() method is very useful because multiple (key-value) pair elements can be inserted into a dictionary, all at once.
Merge Two Nested Dictionaries with dict.update()
Example on how to merge two nested dictionaries using the Python dictionary method dict.update():
company_1 = {'id_1': {'name': 'john', 'profession': 'electrician'}, 'id_2': {'name': 'kim', 'profession': 'plumber'}} company_2 = {'id_3': {'name': 'tammy', 'profession': 'mason'}, 'id_4': {'name': 'lily', 'profession': 'welder'}} company_merge = company_1.update(company_2) print(company_merge)Output:
NoneAttempted merging of nested dictionaries failed, resulting in a identities = {'id_1': 'jim', 'id_2': 'tammy', 'id_3': 'sarah', 'id_4': 'bob'} change_id_3 = {'id_3': 'amada'} identities.update(change_id_3) print(identities)3 value being returned. But you can see that the original dictionary in {'id_1': 'jim', 'id_2': 'tammy', 'id_3': 'amada', 'id_4': 'bob'} 2 has changed:
def update_nested_dict(existing, new): """Nested update of python dictionaries for config parsing Adapted from //stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth """ for k, v in new.iteritems(): if isinstance(existing, collections.Mapping): if isinstance(v, collections.Mapping): r = update_nested_dict(existing.get(k, {}), v) existing[k] = r else: existing[k] = new[k] else: existing = {k: new[k]} return existingThe 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:
Do comment if you have any doubts and suggestions on this Python dictionary topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Rohit
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.