• 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.  How to Check if Tuple is Empty in Python
  • 2.  Using Selenium to Get Text from Element in Python
  • 3.  Get Substring from String in Python with String Slicing
  • 4.  Selenium maximize_window() Function to Maximize Window in Python
  • 5.  Sorting with Lambda Functions in Python
  • 6.  Check if String Contains Numbers in Python
  • 7.  Inverting Dictionary Variables in Python with Dictionary Comprehension
  • 8.  pandas max – Find Maximum Value of Series or DataFrame
  • 9.  How to Use Python to Remove Zeros from List
  • 10.  Flatten List of Tuples in Python

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

x