Merging Python Dictionaries Using Dictionary Unpacking

By Hemanta Sundaray on 2021-08-27

We can merge two or more Python dictionaries using a feature known as dictionary unpacking.

Example:

inventory_formal = {'Suit': 100, 'Jacket': 75}

inventory_casual = {'Shirt': '200', 'Chino': 100}

inventory = {**inventory_formal, **inventory_casual}

print(inventory)
# {'Suit': 100, 'Jacket': 75, 'Shirt': '200', 'Chino': 100}

Starting from Python 3.9, we can merge and update Python dictionaries using the | (bitwise OR operator). Learn more in my blog post here.

Join the Newsletter