How to Merge Python Dictionaries
Tags: python, programming, tutorial, beginners
Published: 2023-03-28 08:24:51

In this tutorial, you will learn different types of method to [merge python dictionaries](https://www.programdoc.com/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.
```py
# 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)
```
**Output**
```py
{'a': 10, 'b': 20, 'c': 50, 'd': 40, 'e': 60}
```

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.
```py
# 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)
```
**Output**
```py
{'a': 10, 'b': 20, 'c': 50, 'd': 40, 'e': 60}
```

## Dictionary using merge using pipe (|) operator
```py
# 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)
```
**Output**
```py
{'a': 10, 'b': 20, 'c': 50, 'd': 40, 'e': 60}
```

## Merge dictionary using double star (**) operator
This operator is used to unpack the dictionary values
```py
k = lambda a, b, c: print(a, b, c)

dict1 = {'a': 10, 'b': 20, 'c': 30}
k(**dict1)
```
**Output**
```py
10 20 30
```
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.
```py
# 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)
```
**Output**
```py
{'a': 10, 'b': 20, 'c': 50, 'd': 40, 'e': 60}
```


## 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.

```py
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()))
```
**Output**
```py
<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]
```
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](https://dev.to/max24816/python-list-vs-tuple-1fk0)
[Python dictionary append](https://dev.to/max24816/python-dictionary-append-1an7)
[Python One Line While Loop](https://dev.to/max24816/python-one-line-while-loop-4j2k)
[Read and write csv file in Python](https://dev.to/max24816/python-read-and-write-csv-file-2onh)
[Python Install Jupyter Notebook](https://dev.to/max24816/install-jupyter-notebook-1cm0)
[Python Create Virtual Environment](https://dev.to/max24816/python-create-virtual-environment-3iai/edit)