Methods to merge two or more dictionaries in python
In this article, we discuss how to merge two or more dictionaries in python.
-
Method 1- Using update() method
Before understanding this let us understand how the update() method works in python.
Update() method in python
update() method is to update one of the dictionaries with the key and value of another dictionary. If the key is not present in one dictionary then that key will be added in the dictionary otherwise if the key is already present then that key-value pair is updated in the dictionary.
Syntax: d1.update(d2)
Let us understand this with the help of an example.
d1={"a":1,"b":2,"c":3,"e":5} d2={"c":4,"f":6} d1.update(d2) print(d1)
Output
{'a': 1, 'b': 2, 'c': 4, 'e': 5, 'f': 6}
Explanation
In this example we see that key “c” is present in both d1 and d2 hence this value at this key is updated while other key-value normally add in the dictionary. The second thing we noticed that it is an in-place method that means no new dictionary is returned by the method and the changes are done in the dictionary itself.
- Python Program to Find Product of Values of elements in a Dictionary
- Python Data Presistence – Methods of Built-in Data Type Classes
- Python Program to Count the Number of Each Vowel
We see that the update method easily merges two dictionaries. So this is how the update method work.
-
Method 2-Using **kwargs
Before understanding this method let us see how **kwargs
works in python.
**Kwargs
**Kwargs in python means keyword argument i.e. is used to pass a keyworded, variable-length argument list. **
allows us to pass multiple arguments to a function. This argument creates a dictionary inside the function and then expands it. Let us understand this with an example.
d1={"a":1,"b":2,"c":3,"e":5} d2={"c":4,"f":6} d3={**d1,**d2} print(d3)
Output
{'a': 1, 'b': 2, 'c': 4, 'e': 5, 'f': 6}
Explanation
**d1 & **d2 expanded the contents of both the dictionaries to a collection of key-value pairs.
d3={"a":1,"b":2,"c":3,"e":5,"c":4,"f":6}
This method work in this way. When we use ** with a dictionary it expands like this as shown above. Here we also see that key “c” is common in both the dictionary hence key-value pair of one dictionary gets updated with another dictionary.
Note: We can pass as many as an argument in this method.
d1={"a":1,"b":2,"c":3,"e":5} d2={"c":4,"f":6} d3={"g":7,"h":8} d4={"i":9,"c":10,"k":11} d5={**d1,**d2,**d3,**d4} print(d5)
Output
{'a': 1, 'b': 2, 'c': 10, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'k': 11}
Here we pass 4 arguments and we get the perfect result. This is also one of the main advantages of this method.
So these are the methods to merge two or more dictionaries in python.
Problem with these methods and their solution
In all the method that we discussed till now, we have faced an issue that if the key we get in two or more dictionaries then the key-value get updated. This can be a major issue if we want to take account of all the key-value pairs in the dictionary. There is no specific method to solve this problem but with our knowledge of python programming, we can solve this issue and also make a user-defined method for this.
d1={"a":1,"b":2,"c":3,"e":5} d2={"c":4,"f":6} d3 = {**d1, **d2} for key, value in d3.items(): if key in d1 and key in d2: d3[key] = [value , d1[key]] print(d3)
Output
{'a': 1, 'b': 2, 'c': [4, 3], 'e': 5, 'f': 6}
Explanation:
First, we de-serialize the contents of the dictionary to a collection of key/value pairs and store it in d3 as seen before. Then we traverse through the elements of the dictionary d3 and check if we get the same key multiple times. If yes then we can store them in the list and our work will be done.