Python – Merge Dictionaries

Python – Merge dictionaries

Merging of two dictionaries is creating a new dictionary with the key-value pairs from both the given dictionaries.

There are many ways in which we can merge to dictionaries.

In this tutorial, will learn how to merge two dictionaries using OR bitwise operator |, or dictionary update() method.

1. Exceptional Scenario: Two dictionaries have common keys

If the two dictionaries have any common keys, then we have to make a decision of which dictionary has to be prioritised.

For example, if dictionary dict1 has key1, and key2 as keys, and dictionary dict2 has key2, and key3 as keys, we have a common key key2 between the two dictionaries.

key2 has a chance of having a different value in dict1 and dict2.

Now, we need to make a decision of which value must be there for key2 in the merged dictionary.

2. Merge dictionaries using Dictionary update() method in Python

Syntax

The syntax to merge two dictionaries dict1 and dict2 using update method is

dict1.update(dict2)

The above statement updates the key-values pairs in dict1 with the key-value pairs from dict2. It inserts key-value pairs of dict2 to dict1 for the keys that are not present in dict1. And for the common keys between the two dictionaries, the values from dict2 would be considered and updated for the keys in dict1.

If you would like to prioritise the values from dict1, but not dict2, then swap between the calling dictionary and argument dictionary as shown in the following expression.

dict2.update(dict1)

Example

In the following program, we take two dictionaries: dict1 and dict2. We shall merge these two dictionaries using merge() method of dictionary class.

Python Program

dict1 = {
    "key1": 10,
    "key2": 20,
    "key3": 30}
 
dict2 = {
    "key5": 50,
    "key2": 2000,
    "key4": 40}

dict1.update(dict2)

print(dict1)
Run Code Copy

Output

{'key1': 10, 'key2': 2000, 'key3': 30, 'key5': 50, 'key4': 40}

For the keys, key4 and key5, that are not present in dict1, new entries are created. And for the common keys, the values from dict2 are updated in dict1.

Please note that this operator updates the calling dictionary dict1. If you would like to keep the original dictionaries unchanged, copy the contents of calling dictionary, and then make the update.

In the following program, we make a copy of the dict1 to result, and then update the result with key-value pairs from dict2 using update() method.

Python Program

dict1 = {
    "key1": 10,
    "key2": 20,
    "key3": 30}
 
dict2 = {
    "key5": 50,
    "key2": 2000,
    "key4": 40}

result = dict1.copy()

result.update(dict2)

print(result)
Run Code Copy

Output

{'key1': 10, 'key2': 2000, 'key3': 30, 'key5': 50, 'key4': 40}

Now, we have the two dictionaries merged and the result stored in a separate dictionary, rather than updating the original dictionaries.

3. Merge Dictionaries using OR Operator in Python

Syntax

The syntax to merge two dictionaries dict1 and dict2 using OR bitwise operator is

dict1 | dict2

The above expression creates a new dictionary with the key-value pairs of dict1 and inserts key-value pairs of dict2 to dict1 for the keys that are not present in dict1.

And for the common keys between the two dictionaries, the values from dict2 would be considered.

If you want to prioritise the values in dict1 to dict2, then change the order of the dictionaries as shown in the following expression.

dict2 | dict1

Example

In the following program, we take two dictionaries: dict1 and dict2. We shall merge these two dictionaries using OR bitwise operator.

Python Program

dict1 = {
    "key1": 10,
    "key2": 20,
    "key3": 30}
 
dict2 = {
    "key5": 50,
    "key2": 2000,
    "key4": 40}

result = dict1 | dict2

print(result)
Run Code Copy

Output

{'key1': 10, 'key2': 2000, 'key3': 30, 'key5': 50, 'key4': 40}

For the keys, key4 and key5, that are not present in dict1, new entries are created. And for the common keys, the values from dict2 are updated in dict1.

Summary

In this tutorial, we learned how to merge two dictionaries, and addressed the scenarios where the two dictionaries can have common keys, using dictionary update() method or OR bitwise operator.

Related Tutorials

Code copied to clipboard successfully 👍