Python Dictionary update() method updates the dictionary with the items from another dictionary, an iterable of key-value pairs, or keyword arguments into the calling dictionary.
If a key already exists, its value will be overwritten with the new value; if not, a new key-value pair is added.
dict1 = {'Flower': 'Rose', 'Fruit': 'Apple', 'Bird': 'Parrot'} dict2 = {'Animal': 'Tiger', 'Tree': 'Coconut'} print("Original dictionary1: ", dict1) # Output: {'Flower': 'Rose', 'Fruit': 'Apple', 'Bird': 'Parrot'} print("Original dictionary2: ", dict2) # Output: {'Animal': 'Tiger', 'Tree': 'Coconut'} dict1.update(dict2) print("Updated dictionary1: ", dict1) # Output: {'Flower': 'Rose', 'Fruit': 'Apple', 'Bird': 'Parrot', 'Animal': 'Tiger', 'Tree': 'Coconut'}
In Python 3.9, the | operator was introduced, which creates a new dictionary by merging (e.g., d1 | d2), whereas update() is an in-place operation, similar to d1 |= d2.
Use update() for efficiency when the original reference must be preserved.
Syntax
dict1.update(iterable)
Parameters
Argument | Description |
iterable(optional) |
It represents a dictionary or an iterable object with key/value pairs. If it is an iterable of tuples or lists, each element is a 2-item sequence (key, value), e.g., [(‘a’, 1), (‘b’, 2)]. |
Updating a dictionary with key-values
In the above figure, we defined a dictionary with one element and used the update() method to add new key-value elements and change the existing property as well.
The update() method can accept keyword arguments directly to add/overwrite string-key values to an existing dictionary.
main_dict = {'Name': 'Krunal'} print("Original dictionary:", main_dict) # Output: Original dictionary: {'Name': 'Krunal'} main_dict.update(Name='Ankit', Rollno=21, College='VVP') print("After updating a dictionary", main_dict) # Output: After updating a dictionary {'Name': 'Ankit', 'Rollno': 21, 'College': 'VVP'}
In this code, we updated the main_dict with three key-value pairs. The first property is Name=Krunal.
We are now updating this property with the name=Ankit and adding two new key-value pairs: Rollno=21 and College=VVP.
So, the final updated main_dict contains three elements.
Non-2-Item Iterable
If you attempt to use a non-2-Item Iterable in the update() method, it will throw an error.
dict = {} try: dict.update([('a', 1), ('b')]) # Second item has length 1 except ValueError as e: print(e) # Output: dictionary update sequence element #1 has length 1; 2 is required
Updating the dictionary if a key exists
What if the key already exists while updating a dictionary? In that case, it will override the existing values with new ones.
origin_dict = {'a': 1, 'b': 2} print(origin_dict) # Output: {'a': 1, 'b': 2} new_dict = {'a': 0, 'b': 0} origin_dict.update(new_dict) print(origin_dict) # Output: {'a': 0, 'b': 0}
Using a tuple iterable
We can add new keys from the list of tuples using the update() method, which works with any iterable that yields 2-item sequences.
dict = {'x': 21} list_of_tuples = [('y', 31), ('z', 0)] dict.update(list_of_tuples) print(dict) # Output: {'x': 21, 'y': 31, 'z': 0}
Updating an empty dictionary
If the input dictionary is empty, we can update it with new values as they are populated efficiently.
empty_dict = {} print(empty_dict) # Output: {} filled_dict = {'name': 'KDL'} empty_dict.update(filled_dict) print(empty_dict) # Output: {'name': 'KDL'}
That’s it.