How to Merge Dictionaries in Python (3 Examples)

Merge Python dictionaries with the dictionary.update() method.

The most convenient way to merge two dictionaries in Python is by using the dict.update() method:

dict1.update(dict2)

This code merges dict1 at the end of dict2. Notice that it doesn’t create a new dictionary.

This approach works with Python 2 and Python 3, so it’s the “safest bet” when merging two dictionaries. Besides, there are other modern approaches to merging dictionaries too.

This is a short but comprehensive guide to merging dictionaries in Python.

Method 1: The update() Method

Calling the dict.update method to merge two dictionaries

Using update() method to merge dictionaries adds a dictionary to the end of another one.

To merge two dictionaries using the update() method, you need to have two dictionaries. Then call .update() on one of the dictionaries and pass the other into the call.

For example, let’s create two dictionaries dict1 and dict2 and merge the second one into the first one:

dict1 = {'a': 1, 'b': 2}
dict2 = {'d': 3, 'c': 4}

dict1.update(dict2)

print(dict1)

Output:

{'a': 1, 'b': 2, 'd': 3, 'c': 4}

But this way you modify one of the original dictionaries. To avoid this, you should create a function that takes two dictionaries, merges them, and returns a new one.

Consider implementing the following function into your codebase:

def merge_dicts(a, b):
    c = a.copy()
    c.update(b)
    return c

This function copies the contents of the dictionary a, merges it with b, and returns the result. The original dictionary stays the same.

Now you can use this approach to merge two dictionaries:

dict1 = {'a': 1, 'b': 2}
dict2 = {'d': 3, 'c': 4}

dict3 = merge_dicts(dict1, dict2)

print(dict3)

Output:

{'a': 1, 'b': 2, 'd': 3, 'c': 4}

Method 2: Double Star **

Merging Python dictionaries with the ** operator

As of Python 3.5, it has been possible to use double star (**) to merge two dictionaries.

dict1 = {'a': 1, 'b': 2}
dict2 = {'d': 3, 'c': 4}

dict3 = {**dict1, **dict2}

print(dict3)

Output:

{'a': 1, 'b': 2, 'd': 3, 'c': 4}

This works because the ** operator unpacks the contents of the dictionaries. It creates a new dictionary based on the elements of the two unpacked dictionaries.

To better understand how it works, please refer to **kwargs in Python.

13 Python Tricks and Tips [in 2023]

Method 3: The | Operator (Python 3.9+ Only)

Merging Python dictionaries with the pipe operator |

This is a fairly new approach to merging dictionaries in Python introduced in Python version 3.9.

To merge two (or more) dictionaries, you can use the | operator between dictionaries to merge them. This operation creates a completely new dictionary that combines the old dictionaries.

For instance:

dict1 = {'a': 1, 'b': 2}
dict2 = {'d': 3, 'c': 4}

dict3 = dict1 | dict2

print(dict3)

Output:

{'a': 1, 'b': 2, 'd': 3, 'c': 4}

Conclusion

Today you learned how to merge dictionaries in Python.

To take home, there are three ways you can use to merge dictionaries:

  1. The dict.update() method.
  2. The ** operator
  3. The | operator (in Python 3.9+)

The most convenient and applicable way is by using the dict.update() method. This method works in most Python versions and is readable and intuitive to use.

Thanks for reading. Happy coding!

Further Reading