In this article you can see how to merge dictionaries in Python 2 & 3 explained for beginners in several different situations:

The post is updated with features and functions from the latest python versions.

Python dictionary merge with unpacking generalizations (since 3.5)

For the latest python versions you have several options to merge two and more dictionaries depending on your needs: repetition, compatibility with python 2,performance. In this section you can see how to merge dictionaries using the new feature: PEP 448 -- Additional Unpacking Generalizations:

dictA = {'Java': 1, 'Python': 2, 'C++': 3}
dictB = {'PHP': 2, 'GO': 4, 'Fortran': 5}
dictC = {**dictA, **dictB}
print(dictC)

result:

{'Java': 1, 'Python': 2, 'C++': 3, 'PHP': 2, 'GO': 4, 'Fortran': 5}

Note: if there are repetitions of keys in the dictionary the values will be updated. The following example demonstrate this:

dictA = {'Java': 1, 'Python': 2, 'C++': 3}
dictB = {'Python': 3, 'C++': 4, 'Fortran': 5}
dictC = {**dictA, **dictB}
print(dictC)
dictC = {**dictB, **dictA}
print(dictC)

result:

{'Java': 1, 'Python': 3, 'C++': 4, 'Fortran': 5}
{'Python': 2, 'C++': 3, 'Fortran': 5, 'Java': 1}

As you can see the order of the dictionaries matters. And the values of the second one overrides the values of the first one!

Python dictionary merge more than two dictionaries

The same feature: PEP 448 -- Additional Unpacking Generalizations gives you elegant way of merging two and more dictionaries. The syntax for merging 3 dictionaries with override of the values is below:

dict1 = {'Java': 1, 'Python': 2, 'C++': 3}
dict2 = {'PHP': 2, 'GO': 4, 'Fortran': 5}
dict3 = {'Scala': 6, 'Ruby': 4, 'Javascript': 5}
dict4 = {**dict1, **dict2, **dict3}
print(dict4)

result:

{'Java': 1, 'Python': 2, 'C++': 3, 'PHP': 2, 'GO': 4, 'Fortran': 5, 'Scala': 6, 'Ruby': 4, 'Javascript': 5}

The same apply for the override of the values from the previous section. It's important to say that values are overridden from left to right - if there is a value in the 3 dictionaries - then the last one will override the previous.

Python simple dictionary merge

A simple yet efficient way to merge dictionaries is by using method update. The advantage of this method is simplicity and it's easy to be understand by beginners:

dictA = {'Java': 1, 'Python': 2, 'C++': 3}
dictB = {'PHP': 2, 'GO': 4, 'Fortran': 5}

dictA.update(dictB)
print(dictA)

result:

{'Java': 1, 'Python': 2, 'C++': 3, 'PHP': 2, 'GO': 4, 'Fortran': 5}

If you have repetitions they will be updated too:

dictA = {'Java': 1, 'Python': 2, 'C++': 3}
dictC = {'Python': 3, 'C++': 4, 'Fortran': 5}

dictA.update(dictC)
print(dictA)

result:

{'Java': 1, 'Python': 3, 'C++': 4, 'Fortran': 5}

Python 2 & 3 dictionary merge with override of values

If you need a custom solution which will cover some special needs - removing repetitions, filtering of values etc. then you can write a method. Next example show a simple method compatible with Python 2 and 3:

def merge_two_dicts(dictA, dictB):
    dictC = dictA.copy()
    dictC.update(dictB)
    return dictC

dictA = {'Java': 1, 'Python': 2, 'C++': 3}
dictB = {'Python': 3, 'C++': 4, 'Fortran': 5}
dictC = merge_two_dicts(dictA, dictB)
print(dictC)

result:

'Java': 1, 'Python': 3, 'C++': 4, 'Fortran': 5}

Here the values are updated. If you want to skip override then check next section.

Python 2 & 3 dictionary merge no override

Sometimes you don't want to update values if the key is present in the dictionary. In Python you can setup a custom method and lambda expression which will filter the existing keys. This line of code make "left outer join" of the dictionaries:

dictC = {x: dictB[x] for x in dictB if x not in dictA}

if you want to have the intersection of the dictionaries this is the line of code to be used:

dictC = {x: dictB[x] for x in dictB if x in dictA}

and finally the example which merge dictionaries without override of existing keys

def merge_two_dicts_rep(dictA, dictB):
    dictC = {x: dictB[x] for x in dictB if x not in dictA}
    dictC.update(dictA)
    return dictC

dictA = {'Java': 1, 'Python': 2, 'C++': 3}
dictB = {'Python': 3, 'C++': 4, 'Fortran': 5}
dictC = merge_two_dicts_rep(dictA, dictB)
print(dictC)

result:

{'Fortran': 5, 'Java': 1, 'Python': 2, 'C++': 3}

Python 2 merge of dictionaries

For python 2 you can find another way of merging dictionaries by joining them:

dictA = {'Java': 1, 'Python': 2, 'C++': 3}
dictB = {'PHP': 2, 'GO': 4, 'Fortran': 5}
dictC = dict(dictA.items() + dictB.items())
# print(dictC) 

result:

{'Java': 1, 'Python': 2, 'C++': 3, 'Fortran': 5, 'GO': 4, 'PHP': 2}

Merge dictionaries with itertools

Using module itertools and method chain allow merge of dictionaries in a single line:

import itertools
dictA = {'Java': 1, 'Python': 2, 'C++': 3}
dictB = {'PHP': 2, 'GO': 4, 'Fortran': 5}

dictC = dict(itertools.chain(dictA.items(), dictB.items()))
print(dictC)

result:

{'Java': 1, 'Python': 2, 'C++': 3, 'PHP': 2, 'GO': 4, 'Fortran': 5}

Again the same apply for repeting elements(override of the values). So for input:

dictA = {'Java': 1, 'Python': 2, 'C++': 3}
dictB = {'Python': 3, 'C++': 4, 'Fortran': 5}

the result is:

{'Java': 1, 'Python': 3, 'C++': 4, 'Fortran': 5}

Performance tests

The performance tests show the following results for 1000000 executions:

  • dictC = {**dictA, **dictB} - 4 function calls in 0.418 seconds
  • dictA.update(dictB) - 1000003 function calls in 0.582 seconds
  • dictC = dict(itertools.chain(dictA.items(), dictB.items())) - 2000002 function calls in 2.044 seconds

The performance test code is shown below:

import itertools
import cProfile

def before():
    for i in range(1, 1000000):
        dictA = {'Java': 1, 'Python': 2, 'C++': 3}
        dictB = {'Python': 3, 'C++': 4, 'Fortran': 5}
        dictC = {**dictA, **dictB}


def after():
    for i in range (1, 1000000):
        dictA = {'Java': 1, 'Python': 2, 'C++': 3}
        dictB = {'Python': 3, 'C++': 4, 'Fortran': 5}
        dictA.update(dictB)

def test():
    for i in range (1, 1000000):
        dictA = {'Java': 1, 'Python': 2, 'C++': 3}
        dictB = {'Python': 3, 'C++': 4, 'Fortran': 5}
        dictC = dict(itertools.chain(dictA.items(), dictB.items()))

cProfile.run('before()')
cProfile.run('after()')
cProfile.run('test()')