Home » Programming » Python » Python Merge Dictionaries

How to Merge Dictionaries in Python, With Examples

This article will show you the simplest ways to merge two or more dictionaries in the Python programming language.

Note that the code examples in this article are written for Python version 3 – and will not work with Python version 2!

What is a Dictionary in Python?

We’ve covered what dictionaries are in our article here.

As there are a couple of ways to merge multiple dictionaries in Python, we’ve put together this article separately to make sure we’ve got everything covered!

Merging/Joining Using Dictionary Union Operator (Python 3.9 and Up)

New to Python 3.9, the dictionary union operator lets you merge two dictionaries in a single line. It’s super convenient and is the best way to merge two dictionaries in newer releases of Python.

Python Dictionary Union Operator Syntax

Here’s the syntax for the new dictionary union operator:

mergedDict = dict1 | dict2

Note that:

  • The | (pipe) character acts as the union operator when placed between two dictionaries
  • When a | is placed between two dictionary variables or values, the statement formed will return a new dictionary containing the values of both
  • You can chain union operator statements to join more than two dictionaries conveniently.
    • This is better demonstrated than explained – see the examples below for how it works.

Example – Merging Two Dictionaries

Below, two dictionaries, each describing the attributes of the same cat, are merged into a single resulting dictionary using the dictionary union operator:

myDictionary = {
    "name": "Fred",
    "animal": "cat",
    "colour": "red",
    "age": 3
} 

myOtherDictionary = {
    "size": "small",
    "temperament": "angry",
    "claws": "sharp"
}

mergedDictionary = myDictionary | myOtherDictionary

print(mergedDictionary)

The above will print the merged dictionary:

{
    "name": "Fred",
    "animal": "cat",
    "colour": "red",
    "age": 3,
    "size": "small",
    "temperament": "angry",
    "claws": "sharp"
}

Example – Chaining to Merge More than Two Dictionaries

Multiple dictionary union operations can be chained together to merge as many dictionaries as is required:

myDictionary = {
    "name": "Fred",
    "animal": "cat",
    "colour": "red",
    "age": 3
} 

myOtherDictionary = {
    "size": "small",
    "temperament": "angry",
    "claws": "sharp"
}

myThirdDictionary = {
    "eyes": "blue",
    "teeth": "also sharp"
}

mergedDictionary = myDictionary | myOtherDictionary | myThirdDictionary

print(mergedDictionary)

Above, three dictionaries are merged by simply adding another union operator to the statement, which creates the merged dictionary.

Unpacking/Enumerating (Python 3.5 and Up)

If you aren’t yet on the latest version of Python 3, you can unpack/enumerate the dictionaries to be merged using the ** statement. This will expand the contents of a given dictionary. By wrapping this statement in a new dictionary, a dictionary containing the values of each unpacked dictionary will be created.

Unpacking to Merge Dictionaries Syntax

Merging dictionaries by unpacking them using the ** statement looks like this:

mergedDict = {**dict1, **dict2}

Example – Merging Two Dictionaries by Unpacking

And here it is in action:

myDictionary = {
    "name": "Fred",
    "animal": "cat",
    "colour": "red",
    "age": 3
} 

myOtherDictionary = {
    "size": "small",
    "temperament": "angry",
    "claws": "sharp"
}

mergedDictionary = {**myDictionary, **myOtherDictionary}

print(mergedDictionary)

Example – Merging Multiple Dictionaries by Unpacking

Like the union operator, unpacking dictionaries to merge them is not limited to just two dictionaries:

myDictionary = {
    "name": "Fred",
    "animal": "cat",
    "colour": "red",
    "age": 3
} 

myOtherDictionary = {
    "size": "small",
    "temperament": "angry",
    "claws": "sharp"
}

myThirdDictionary = {
    "eyes": "blue",
    "teeth": "also sharp"
}

mergedDictionary = {**myDictionary, **myOtherDictionary, **myThirdDictionary}

print(mergedDictionary)

Dictionaries are Ordered

As of Python version 3.7, dictionaries are ordered – the items in them are sequenced in the order they are added.

Thus, when merging dictionaries, the items from the first dictionary will appear first, followed by items from the second, etc.

Conflicts

The merge operations above will overwrite existing values in the order of appearance. A value with the same key in a dictionary in the merge operation will be overwritten by a value at the same key in a subsequent dictionary.

Why Not Use the update() Method?

We’ve covered appending new values to a dictionary using the update() method here – so why not use it?

The update() method will alter the dictionary it is called from, adding the items from another dictionary rather than creating a merged copy.

The update() method can also only accept the values from a single second dictionary to be added to the first, rather than allowing two or more dictionaries to be merged.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment