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.
Leave a Reply