• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Combine Dictionaries in Python

How to Combine Dictionaries in Python

August 15, 2022 Leave a Comment

There are a few ways you can combine dictionaries in Python. The easiest way to merge and combine the keys and values of multiple dictionaries is with the | merge operator. This works with Python 3.9+.

d1 = {'apples': 1, 'bananas': 2}
d2 = {'oranges': 3, 'pears': 4}

d3 = d1| d2

print(d3)

#Output:
{'apples': 1, 'bananas': 2, 'oranges': 3, 'pears': 4}

You can also use unpacking with ** to combine dictionaries.

d1 = {'apples': 1, 'bananas': 2}
d2 = {'oranges': 3, 'pears': 4}

d3 = {**dict1,**dict2}

print(d3)

#Output:
{'apples': 1, 'bananas': 2, 'oranges': 3, 'pears': 4}

One other way to combine and merge dictionaries is with the update() function.

d1 = {'apples': 1, 'bananas': 2}
d2 = {'oranges': 3, 'pears': 4}

d1.update(d2)

print(d1)

#Output:
{'apples': 1, 'bananas': 2, 'oranges': 3, 'pears': 4}

One final way is with a loop which adds each key value pair of a second dictionary to the first dictionary.

d1 = {'apples': 1, 'bananas': 2}
d2 = {'oranges': 3, 'pears': 4}

for key, value in d2.items():
    d1[key] = value

print(d1)

#Output:
{'apples': 1, 'bananas': 2, 'oranges': 3, 'pears': 4}

When working with collections of data, the ability to combine, merge, and concatenate multiple collections to another can be useful.

One such case is if you want to combine dictionaries together in Python.

If you are using Python 3.9+, then the easiest way to merge Python dictionaries is with the merge operator |

Below shows you how to use | to merge two dictionaries in Python.

d1 = {'apples': 1, 'bananas': 2}
d2 = {'oranges': 3, 'pears': 4}

d3 = d1| d2

print(d3)

#Output:
{'apples': 1, 'bananas': 2, 'oranges': 3, 'pears': 4}

Note, if there are keys which share the same name, then only the first key value pair will be kept since dictionaries cannot have duplicate keys.

Using Unpacking to Combine Dictionaries in Python

Another way you can combine dictionaries in Python is with item unpacking.

Items of an object in Python can be unpacked using either * or **. For dictionaries, to access both the keys and values of a dictionary, you need to use **.

Item unpacking allows you to combine multiple dictionaries with not much code.

Below shows you how to use unpacking to combine dictionary variables in Python.

d1 = {'apples': 1, 'bananas': 2}
d2 = {'oranges': 3, 'pears': 4}

d3 = {**dict1,**dict2}

print(d3)

#Output:
{'apples': 1, 'bananas': 2, 'oranges': 3, 'pears': 4}

Using update() to Combine Python Dictionaries

Another way you can combine Python dictionaries is with the dictionary update() function.

You can pass a dictionary to update() and update() will add the key value pairs of the passed dictionary to the dictionary using update() in place.

Before shows a simple example of how you can use update() to combine Python dictionaries in place.

d1 = {'apples': 1, 'bananas': 2}
d2 = {'oranges': 3, 'pears': 4}

d1.update(d2)

print(d1)

#Output:
{'apples': 1, 'bananas': 2, 'oranges': 3, 'pears': 4}

Using For Loop to Merge Python Dictionaries

You can also use a for loop to merge multiple dictionaries together in Python.

You can use the items() function to return the items of a dictionary and then loop over each item and assign them to another dictionary.

Below shows you how to use a for loop to combine dictionaries in Python.

d1 = {'apples': 1, 'bananas': 2}
d2 = {'oranges': 3, 'pears': 4}

for key, value in d2.items():
    d1[key] = value

print(d1)

#Output:
{'apples': 1, 'bananas': 2, 'oranges': 3, 'pears': 4}

Hopefully this article has been useful for you to learn how to combine multiple dictionaries in Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Create Empty DataFrame with pandas
  • 2.  Using Python to Capitalize Every Other Letter of String
  • 3.  How to Capitalize the First Letter of Every Word in Python
  • 4.  Python ljust Function – Left Justify String Variable
  • 5.  Get Current URL with Selenium in Python
  • 6.  Get Difference Between datetime Variables in Python
  • 7.  Python asin – Find Arcsine and Inverse Sine of Number Using math.asin()
  • 8.  Remove Time from Datetime in Python
  • 9.  Python rjust Function – Right Justify String Variable
  • 10.  How to Group By Columns and Find Mean in pandas DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy