There are various ways in which Dictionaries can be merged by the use of various functions and constructors in Python. In this article, we will discuss a few ways of merging dictionaries.
Using the method update()
By using the method update() in Python, one list can be merged into another. But in this, the second list is merged into the first list and no new list is created. It returns None.
Example:
Python3
# Python code to merge dict using update() methoddef Merge(dict1, dict2): return(dict2.update(dict1)) # Driver codedict1 = {'a': 10, 'b': 8}dict2 = {'d': 6, 'c': 4}# This return Noneprint(Merge(dict1, dict2))# changes made in dict2print(dict2) |
Output:
None
{'c': 4, 'a': 10, 'b': 8, 'd': 6}
Using ** in Python
This is generally considered a trick in Python where a single expression is used to merge two dictionaries and stored in a third dictionary. The single expression is **. This does not affect the other two dictionaries. ** implies that an argument is a dictionary. Using ** [double star] is a shortcut that allows you to pass multiple arguments to a function directly using a dictionary. For more information refer **kwargs in Python. Using this we first pass all the elements of the first dictionary into the third one and then pass the second dictionary into the third. This will replace the duplicate keys of the first dictionary.
Example:
Python3
# Python code to merge dict using a single # expressiondef Merge(dict1, dict2): res = {**dict1, **dict2} return res # Driver codedict1 = {'a': 10, 'b': 8}dict2 = {'d': 6, 'c': 4}dict3 = Merge(dict1, dict2)print(dict3) |
Output:
{'b': 8, 'a': 10, 'c': 4, 'd': 6}
Using | in Python 3.9
In the latest update of python now we can use “|” operator to merge two dictionaries. It is a very convenient method to merge dictionaries.
Example:
Python3
# code# Python code to merge dict using a single # expression def Merge(dict1, dict2): res = dict1 | dict2 return res # Driver code dict1 = {'x': 10, 'y': 8} dict2 = {'a': 6, 'b': 4} dict3 = Merge(dict1, dict2) print(dict3)# This code is contributed by virentanti16 |
Output:
{'x': 10, 'a': 6, 'b': 4, 'y': 8}
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Merging two list of dictionaries
- Python - Convert Dictionaries List to Order Key Nested dictionaries
- Python | Merging two strings with Suffix and Prefix
- Python | Difference in keys of two dictionaries
- Python | Combine the values of two dictionaries having same key
- Python | Intersect two dictionaries through keys
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python | Sort Python Dictionaries by Key or Value
- Python | Merging duplicates to list of list
- Python | Merging nested lists
- Merging and Updating Dictionary Operators in Python 3.9
- Get() method for dictionaries in Python
- Handling missing keys in Python dictionaries
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Ways to sort list of dictionaries by values in Python – Using itemgetter
- Python | Common items among dictionaries
- Python | Set Difference in list of dictionaries
- Python | Split dictionary of lists to list of dictionaries
- Python | Removing dictionary from list of dictionaries
- Python | Sum list of dictionaries with same key
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

