Definition
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 aNone
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:
None
Attempted merging of nested dictionaries failed, resulting in a None
value being returned. But you can see that the original dictionary in company_1
has changed:
print(company_1) # {'id_1': {'name': 'john', 'profession': 'electrician'}, # 'id_2': {'name': 'kim', 'profession': 'plumber'}, # 'id_3': {'name': 'tammy', 'profession': 'mason'}, # 'id_4': {'name': 'lily', 'profession': 'welder'}}