By Hemanta Sundaray on 2021-08-27
The simplest way to update an existing dictionary with the contents of another dictionary is to use the update() method.
doses_finance = {'Alice': 0, 'John': 1}
doses_marketing = {'Rick': 2}
doses_finance.update(doses_marketing)
print(doses_finance)
# {'Alice': 0, 'John': 1, 'Rick': 2}
Note that the update() method modifies the existing dictionary in place and returns no value.
Starting with Python 3.9, we can use the | (bitwise OR) and |= (in-place bitwise OR) operators to perform dictionary merge and update operations.
The resulting dictionary will be a completely new object that will have the keys from the source dictionaries.
doses_finance = {'Alice': 0, 'John': 1}
doses_marketing = {'Rick': 2}
doses_finance_marketing = doses_finance | doses_marketing
print(doses_finance_marketing)
# {'Alice': 0, 'John': 1, 'Rick': 2}
We can update a dictionary with the keys coming from a different dictionary using the in-place bitwise OR (|=) operator.
doses_finance = {'Alice': 0, 'John': 1}
doses_finance |= {'Rick': 2}
print(doses_finance)
# {'Alice': 0, 'John': 1, 'Rick': 2}