DEV Community

Max
Max

Posted on

How to Merge Python Dictionaries

In this tutorial, you will learn different types of method to merge python dictionaries

Note:

  1. Key value c is used in both the dictionary variables. While merging, dictionary value of the key c will be updated by the second variable value.
  2. In collection ChainMap, it will consider the key of the occurrence first if the key is in both dictionary.

Python merge dictionary using loop

In this example we are using the python for loop to iterate over the dictionary keys, if the key is not in the first variable it will add a new key value pair or if already exists then it will update the value to that respective key.

# Python merge dictionary using loop
dict1 = {'a': 10, 'b': 20, 'c': 30}
dict2 = {'d': 40, 'c': 50, 'e': 60}

for i in dict2.keys():
    dict1[i] = dict2[i]

print(dict1)
Enter fullscreen mode Exit fullscreen mode

Output

{'a': 10, 'b': 20, 'c': 50, 'd': 40, 'e': 60}
Enter fullscreen mode Exit fullscreen mode

Lets do dictionary merge in python way

Dictionary update method

Python have multiple build in dictionary method, one of it is update method which is used to update key values from source to targeted variable.

# Python merge dictionary using inbuild update method
dict1 = {'a': 10, 'b': 20, 'c': 30}
dict2 = {'d': 40, 'c': 50, 'e': 60}

dict1.update(dict2)

print(dict1)
Enter fullscreen mode Exit fullscreen mode

Output

{'a': 10, 'b': 20, 'c': 50, 'd': 40, 'e': 60}
Enter fullscreen mode Exit fullscreen mode

Dictionary using merge using pipe (|) operator

# Python merge dictionary using pipe (|) operator
dict1 = {'a': 10, 'b': 20, 'c': 30}
dict2 = {'d': 40, 'c': 50, 'e': 60}

dict3 = dict1 | dict2

print(dict3)
Enter fullscreen mode Exit fullscreen mode

Output

{'a': 10, 'b': 20, 'c': 50, 'd': 40, 'e': 60}
Enter fullscreen mode Exit fullscreen mode

Merge dictionary using double star (**) operator

This operator is used to unpack the dictionary values

k = lambda a, b, c: print(a, b, c)

dict1 = {'a': 10, 'b': 20, 'c': 30}
k(**dict1)
Enter fullscreen mode Exit fullscreen mode

Output

10 20 30
Enter fullscreen mode Exit fullscreen mode

You can see in the above example, value passed to lambda function is got unpacked to each arguments.
Using this you can perform the merge operation.

# Python merge dictionary using double srar ** operator
dict1 = {'a': 10, 'b': 20, 'c': 30}
dict2 = {'d': 40, 'c': 50, 'e': 60}

dict3 = {**dict1, **dict2}

print(dict3)
Enter fullscreen mode Exit fullscreen mode

Output

{'a': 10, 'b': 20, 'c': 50, 'd': 40, 'e': 60}
Enter fullscreen mode Exit fullscreen mode

Merge dictionary using collection ChainMap

ChainMap will groups multiple dictionary or other mappings together to create a single updatable object. ChainMap is an object under collections not a dict.

from collections import ChainMap
dict1 = {'a': 10, 'b': 20, 'c': 30}
dict2 = {'d': 40, 'c': 50, 'e': 60}

dictChain = ChainMap(dict1, dict2)

print(type(dictChain))
print(dictChain)

dictChain["a"] = 100
dictChain["c"] = 300

print(dictChain)
print("KEYS:", list(dictChain.keys()))
print("VALUES:", list(dictChain.values()))
Enter fullscreen mode Exit fullscreen mode

Output

<class 'collections.ChainMap'>
ChainMap({'a': 10, 'b': 20, 'c': 30}, {'d': 40, 'c': 50, 'e': 60})
ChainMap({'a': 100, 'b': 20, 'c': 300}, {'d': 40, 'c': 50, 'e': 60})
KEYS: ['d', 'c', 'e', 'a', 'b']
VALUES: [40, 300, 60, 100, 20]
Enter fullscreen mode Exit fullscreen mode

You can see in the above example value update to key c only update the first occurrence of the key, in other merge methods it takes the second occurrence of the key.

Explore Other Dev.to Articles

Python list vs tuple
Python dictionary append
Python One Line While Loop
Read and write csv file in Python
Python Install Jupyter Notebook
Python Create Virtual Environment

Top comments (0)