Merge Multiple Dictionaries in a Single Expression in Python

2 min read .

Merging dictionaries is a common task in Python programming, especially when you’re working with data from different sources. In Python, dictionaries are one of the most powerful and flexible data structures, allowing you to store key-value pairs. We’ll explore several ways to merge multiple dictionaries into one using a single expression. Whether you’re a beginner or an experienced Python developer, this guide will help you understand the most efficient methods available in Python.

1. Using the ** Operator (Python 3.5+)

Starting from Python 3.5, you can use the ** unpacking operator to merge dictionaries in a single expression. This method is both concise and readable.

Example:

dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
dict3 = {"d": 5}

merged_dict = {**dict1, **dict2, **dict3}
print(merged_dict)

In this example, the ** operator unpacks the dictionaries into a new dictionary. If there are duplicate keys, the values from the latter dictionaries overwrite those from the former ones, as seen with the key "b".

2. Using dict.update() Method

Another method to merge dictionaries is using the dict.update() method. This method updates the dictionary in place and can be combined with dictionary unpacking for a single expression.

Example:

dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
dict3 = {"d": 5}

merged_dict = dict1.copy()
merged_dict.update(dict2)
merged_dict.update(dict3)

print(merged_dict)

This method first creates a copy of dict1 and then updates it with the contents of dict2 and dict3. While this approach is clear, it requires multiple calls to update(), so it’s not strictly a single expression, but it’s still a very practical approach.

3. Using the {**d1, **d2} Expression (Python 3.9+)

Starting with Python 3.9, the syntax for merging dictionaries has been further simplified. You can use the | operator to merge dictionaries in a single expression.

Example:

dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
dict3 = {"d": 5}

merged_dict = dict1 | dict2 | dict3
print(merged_dict)

The | operator merges the dictionaries similarly to the ** operator, providing a more intuitive and modern syntax.

4. Using collections.ChainMap (For Advanced Use Cases)

For more advanced use cases where you need a view over multiple dictionaries without actually merging them, you can use collections.ChainMap. This is especially useful when you want to treat multiple dictionaries as one without creating a new dictionary.

Example:

from collections import ChainMap

dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
dict3 = {"d": 5}

merged_dict = ChainMap(dict1, dict2, dict3)
print(merged_dict)

Although ChainMap doesn’t actually merge the dictionaries, it provides a view that behaves as if the dictionaries were merged. This is a powerful tool when you want to keep the original dictionaries unchanged.

Conclusion

Merging multiple dictionaries into one is a common task in Python, and there are several ways to do it depending on your needs and the version of Python you’re using. The ** unpacking operator is a concise and effective method for most cases, while the | operator introduced in Python 3.9 offers a more modern approach. For advanced scenarios, ChainMap provides a way to work with multiple dictionaries as a single unit without actual merging.

Tags:
Python

See Also

chevron-up