Python Merge Two Dictionaries into a single Dictionary Example

This tutorial explains How to merge multiple dictionaries into a single dictionary in Python.

The dictionary contains keys and values and represents an object in Python.

For example, two dictionaries contain the following data

basics = {'id': 1, 'name': 'john'}
additional = {'dept': 'sales',
              'role':'sales',
              'salary': 4000}

The merge of these dictionaries results in the new dictionary with keys and values merged. additional dictionary merged into the basic dictionary by overwriting the key and value pairs.

{ "id": 1, "name": "john", "dept": "sales", "role": "sales", "salary": 4000 }

Python Merge Dictionary Example

There are multiple ways we can do

  • use Union Operator

PEP 584 union operator(|) introduced in Python 3.9 version It introduces the merge (|) or update (|=) operator on the dict object.

merge (|) operator merges one into another dict class by overriding the objects.

basics = {'id': 1, 'name': 'john'}
additional = {'dept': 'sales',
              'role':'sales',
              'salary': 4000}
emp = basics | additional
print(emp)

It is easy and the performance is good.

  • use Dictionary Pack and Unpack Syntax

    3.5 version introduced dict pack feature🔗 allows you to do following things *- packing **- unpacking

**dict unpack the key and values and append them into another dict object.

basics = {'id': 1, 'name': 'john'}
additional = {'dept': 'sales',
              'role':'sales',
              'salary': 4000}
emp = {**basics,**additional}
print(emp)
  • Use copy and update.

Itis a merge dict into another in Python 2.x version

Create a result dictionary by copying all data of the first dictionary using the copy() function. Next, update the result dictionary with the second dictionary using the update() function.

basics = {'id': 1, 'name': 'john'}
additional = {'dept': 'sales',
              'role':'sales',
              'salary': 4000}

emp = basics.copy()
emp.update(additional)

print(emp)
  • use iterate dict with items function

Another is to iterate dict using items and convert it into the list append the list of both dictionaries and pass this to the dict constructor

basics = {'id': 1, 'name': 'john'}
additional = {'dept': 'sales',
              'role':'sales',
              'salary': 4000}

emp=dict(list(basics.items()) + list(
    additional.items()))# python 3
#emp1=dict(basics.items() +additional.items()) #For Python 2
print(emp)
  • Use collection ChainMap grouping

The collection package contains the ChainMap function to group multiple dictionaries into a single dict object. Single Empty dict is provided.

from collections import ChainMap

basics = {'id': 1, 'name': 'john'}
additional = {'dept': 'sales',
              'role':'sales',
              'salary': 4000}

emp = dict(ChainMap({}, basics, additional))
print(emp)

It uses mapping by reference, if one of the objects is modified, It gets reflected in the result.

Conclusion

Python 3.9 Join operator is preferable over other approaches in terms of performance and concise and readable, simple syntax.