Merge Two Python Dictionaries in a Single Expression



In Python, merging dictionaries is a common task that combines key-value pairs from two or more dictionaries into a single dictionary. If duplicate keys exist, the values from the second dictionary will typically take precedence when merging.

Python offers several methods for merging dictionaries, each with its characteristics. Below are some common methods:

Using double asterisk (**) Operator

We can combine two dictionaries in Python using the double asterisk (**) to unpack their contents into a new dictionary. This method allows you to merge the key-value pairs from both dictionaries easily.

If there are duplicate keys, the values from the second dictionary will overwrite those from the first. Additionally, the double asterisk(**) has other uses in Python, such as representing an exponential operator and unpacking arguments in functions.

Example

In the following example, the key 'b' appears in both dictionaries, but the value from `dict2` (30) replaces the value from `dict1` (20).

dict1 = {'a': 10, 'b': 20}
dict2 = {'b': 30, 'c': 40}

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

Output

{'a': 10, 'b': 30, 'c': 40}

Using update() Method

The update() method is a built-in method that you can use to add data to dictionaries and also provides a convenient way to merge multiple dictionaries. It modifies the first dictionary in place by adding key-value pairs from the second dictionary.

Example

The following example demonstrates how the update() method can be used to efficiently merge dictionaries and handle keys by updating existing keys.

dict1 = {'a': 10, 'b': 20}
dict2 = {'b': 30, 'c': 40}

#Using update() Method
dict1.update(dict2)
print(dict1)

Output

{'a': 10, 'b': 30, 'c': 40}

Using collections.ChainMap

The ChainMap class in Python, allows you to combine multiple dictionaries or mappings into a single object. If you don't provide any dictionaries, ChainMap will create an empty dictionary by default, ensuring there's always at least one mapping. You can find this ChainMap class in the 'collections' module.

Example

The following example demonstrates using ChainMap from the collections module to combine two dictionaries.

from collections import ChainMap

dict1 = {'key1': 10, 'key2': 20}
dict2 = {'key3': 30, 'key4': 40}
merged_dict = ChainMap(dict1, dict2)
print(merged_dict)

Output

ChainMap({'key1': 10, 'key2': 20}, {'key3': 30, 'key4': 40})

Using | merge Operator

Using the merge (|) operator between two dictionaries creates a new dictionary that combines the key-value pairs from both. If there are duplicate keys, the values from the second dictionary will override those from the first.

Example

Here's a simple example to illustrate how the | operator can be used to merge two dictionariespython

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

merged_dict = dict1 | dict2
print(merged_dict) 

Output

The output of this code will be:

{'a': 1, 'b': 3, 'c': 4}
Updated on: 2025-04-21T11:02:33+05:30

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements