Merge two dictionaries and sum the values in Python

avatar

Borislav Hadzhiev

Last updated: Jul 4, 2022

banner

Photo from Unsplash

Merge two dictionaries and sum the values in Python #

To merge two dictionaries and sum the values:

  1. Use a dict comprehension to iterate over one of the dictionaries.
  2. On each iteration, use the dict.get() method to sum the values.
  3. Specify a default value of 0 in case a key in one dict is not present in the other.
main.py
dict_1 = {'a': 1, 'b': 2, 'c': 3, 'x': 999} dict_2 = {'a': 100, 'b': 100, 'c': 100, 'y': 333} # ✅ merge dictionaries and sum key-value pairs # (includes key-value pairs from dict_1 that are not in dict_2) result_1 = { key: dict_1.get(key, 0) + dict_2.get(key, 0) for key in dict_1 } # 👇️ {'c': 103, 'x': 999, 'a': 101, 'b': 102} print(result_1) # ------------------------------------------------------------------------ # ✅ merge dictionaries and sum key-value pairs (intersection) # only includes key-value pairs that are in both dictionaries result_2 = { key: dict_1.get(key, 0) + dict_2.get(key, 0) for key in set(dict_1) & set(dict_2) } # 👇️ {'b': 102, 'c': 103, 'a': 101} print(result_2) # ------------------------------------------------------------------------ # ✅ merge dictionaries and sum key-value pairs (union) # includes key-value pairs from both dictionaries result_3 = { key: dict_1.get(key, 0) + dict_2.get(key, 0) for key in set(dict_1) | set(dict_2) } # 👇️ {'y': 333, 'x': 999, 'a': 101, 'b': 102, 'c': 103} print(result_3) # ------------------------------------------------------------------------ # ✅ merge dictionaries and sum key-value pairs # (includes key-value pairs from dict_2 that are not in dict_1) result_4 = { key: dict_1.get(key, 0) + dict_2.get(key, 0) for key in dict_2 } # 👇️ {'a': 101, 'b': 102, 'c': 103, 'y': 333} print(result_4)

All of the examples use dict comprehensions to iterate over one of the dictionaries.

Dict comprehensions are very similar to list comprehensions.

They perform some operation for every key-value pair in the dictionary, or select a subset of key-value pairs that meet a condition.

The first example merges the two dictionaries and sums the values for the corresponding keys.

main.py
dict_1 = {'a': 1, 'b': 2, 'c': 3, 'x': 999} dict_2 = {'a': 100, 'b': 100, 'c': 100, 'y': 333} result_1 = { key: dict_1.get(key, 0) + dict_2.get(key, 0) for key in dict_1 } # 👇️ {'c': 103, 'x': 999, 'a': 101, 'b': 102} print(result_1)

Since we only iterate over dict_1, we get all of the key-value pairs from dict_1, but notice that we didn't iterate through dict_2 in any way, so the y key is not included in the result.

We used the dict.get() method to access specific keys in each dictionary.

The dict.get method returns the value for the given key if the key is in the dictionary, otherwise a default value is returned.

The method takes the following 2 arguments:

NameDescription
keyThe key for which to return the value
defaultThe default value to be returned if the provided key is not present in the dictionary (optional)

If a value for the default parameter is not provided, it defaults to None, so the get() method never raises a KeyError.

If you need to merge the dictionaries, sum the key-value pairs and only include key-value pairs that are common for both dictionaries, use an intersection with two set objects.

main.py
dict_1 = {'a': 1, 'b': 2, 'c': 3, 'x': 999} dict_2 = {'a': 100, 'b': 100, 'c': 100, 'y': 333} result_2 = { key: dict_1.get(key, 0) + dict_2.get(key, 0) for key in set(dict_1) & set(dict_2) } # 👇️ {'b': 102, 'c': 103, 'a': 101} print(result_2)

We used the set() class to convert the dictionaries to a set object.

Set objects are an unordered collection of unique elements.

main.py
dict_1 = {'a': 1, 'b': 2, 'c': 3, 'x': 999} dict_2 = {'a': 100, 'b': 100, 'c': 100, 'y': 333} # 👇️ {'x', 'a', 'c', 'b'} print(set(dict_1)) # 👇️ {'y', 'a', 'c', 'b'} print(set(dict_2)) # 👇️ {'a', 'c', 'b'} print(set(dict_1) & set(dict_2))
The ampersand & returns a new set with elements that are common to both set objects (intersection).

The result is a dictionary that only includes key-value pairs that are present in both dictionaries.

If you need to merge the dictionaries, sum the key-value pairs and include key-value pairs that are present in either dictionary, use a union with two set objects.

main.py
dict_1 = {'a': 1, 'b': 2, 'c': 3, 'x': 999} dict_2 = {'a': 100, 'b': 100, 'c': 100, 'y': 333} result_3 = { key: dict_1.get(key, 0) + dict_2.get(key, 0) for key in set(dict_1) | set(dict_2) } # 👇️ {'y': 333, 'x': 999, 'a': 101, 'b': 102, 'c': 103} print(result_3)

The pipe | returns a new set with elements from both set objects.

main.py
dict_1 = {'a': 1, 'b': 2, 'c': 3, 'x': 999} dict_2 = {'a': 100, 'b': 100, 'c': 100, 'y': 333} # 👇️ {'b', 'a', 'y', 'x', 'c'} print(set(dict_1) | set(dict_2))

The result is a dictionary that contains key-value pairs that are present in either dict.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.