There are many ways to add two dictionaries Python because this is basic knowledge when working with dictionaries in Python. Let’s learn about some ways to do this, such as using the update() function, | operator and ** operator with the explanation and examples below.
Add two dictionaries Python
Using the update() function
The Dictionary’s items are added to another Dictionary through this update() method. As a result, you may use this method to add two dictionaries to another.
Syntax:
firstDict.update(secondDict)
Parameters:
- firstDict: the first Dictionary you want to add.
- secondDict: the second Dictionary you want to add.
This function is a void function because it updates directly to firstDict.
Python will add the pairs of key-values from secondDict to firstDict and replace any duplicate keys with the value from secondDict when you use the update() method to add the elements from secondDict to firstDict. To learn how to utilize the update() method, see the example below:
firstDict = {"learn": "2022","share":2} secondDict = {"it": None} # Add two dictionaries together firstDict.update(secondDict) print(firstDict)
Output:
{'learn': '2022', 'share': 2, 'it': None}
Using the Union | operator
The + operator works well with List and String to connect two strings or lists together. But we are unable to use the Dictionary in the same way. So, can two dictionaries be combined using a single operator?
The answer is yes. In Python 3.9, we can use Python to combine dictionaries just with the Union | operator.
Syntax:
firstDict | secondDict
Using this operator, you will receive a merged dictionary of firstDict and secondDict. For example:
firstDict = {"learn": "2022","share":2} secondDict = {"it": None,"learn": 2022} # Add two dictionaries together mergedDict = firstDict | secondDict print(mergedDict)
Output:
{'learn': 2022, 'share': 2, 'it': None}
However, this solution only works in Python 3.9 instead of newer versions. Therefore, if you are using Python 3.10, please use the next solution.
Using the double asterisk **
The function definition is where the ** operator (double asterisk) is most commonly utilized. We are able to provide any number of parameters to the function thanks to this unique syntax.
In JavaScript, the** operator is used similarly to the spread operator. If you have acquainted with the … operator in JavaScript, utilizing the ** operator won’t throw you off.
We must place the ** operator before firstDict and secondDict and enclose them in curly braces to join dictionaries. All components included in firstDict and seconDict will be converted into components of the outer {} curly braces. For instance:
firstDict = {"learn": "2022","share":2} secondDict = {"it": None,"learn": 2022} # Add two dictionaries together mergedDict = {**firstDict, **secondDict} print(mergedDict)
Output:
{'learn': 2022, 'share': 2, 'it': None}
As you can see, using this approach also produces the same results as the two others. In fact, we suggest you use this method to add two dictionaries Python because the two others also have their downsides. Moreover, remember that in dictionaries, if there include two duplicate keys with different values, then the latter one will always override the earlier ones. In the above example, the “learn” property in the firstDict (it has value “2022” of type str) has been replaced with the second “learn” property in the secondDict (it now has type int: 2022).
Summary
We have learned how to add two dictionaries Python using three different methods. It would help if you considered that each approach has its pros and cons. We hope you will be successful using our tutorial.
Maybe you are interested:
- Merge two dictionaries and sum the values in Python
- Sum the values in a list of dictionaries in Python

I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R