How To Merge Two Dictionaries And Sum The Values In Python

Merge two dictionaries and sum the values in Python

How to merge two dictionaries and sum the values in Python? Here are three ways that can help you do this. I will introduce you to the union and the collections.Counter() function and the itertools.chain() function.

Merge two dictionaries and sum the values in Python

Use union()

Syntax:

A.union(*other_sets)

Parameters:

  • other_sets: are sets to combine with set A.

The union() returns the elements of set A and the elements of the Sets associated with Set A.

Example:

  • Create two dictionaries with key-value pairs.
  • Use union with the dict.get function to get key-value pairs to form a dictionary and sum the dictionary’s values.
firstDict = {'a': 1, 'b': 2, 'both': 3}
secondDict = {'d': 100, 'e': 200, 'both': 300}

# Use the union with the dict.get function that takes key-value pairs to perform dictionary merge and sum the dictionary values
result = {i: firstDict.get(i, 0) + secondDict.get(i, 0)
     for i in set(firstDict).union(secondDict)}
 
print('Merge two dictionaries and sum the dictionary values:', result)

Output:

Merge two dictionaries and sum the dictionary values: {'e': 200, 'both': 303, 'a': 1, 'b': 2, 'd': 100}

Use the collections.Counter() function

Example:

  • The collections module is a container used to store collections of data such as a list, dict, set, or tuple. Including the Counter function, a subclass of the dictionary object that counts hashtable objects.
from collections import Counter

firstDict = {'a': 1, 'b': 2, 'both': 3}
secondDict = {'d': 100, 'e': 200, 'both': 300}

# Use the Counter function to merge dictionary and sum dictionary values
result = Counter(firstDict) + Counter(secondDict)

print('Merge two dictionaries and sum the dictionary values:', result)

Output:

Merge two dictionaries and sum the dictionary values: Counter({'both': 303, 'e': 200, 'd': 100, 'b': 2, 'a': 1})

Use the itertools.chain() function

Syntax:

itertools.chain(*iterables)

Example:

  • Create two dictionaries.
  • The defaultdict function returns the default value for the dictionary (In my example, I left it as an integer).
  • Variables ‘key’, and ‘val’ store the key-values ​​of the dictionary. These two variables are in the itertools.chain function, then perform dictionary merge and sum the dictionary values.
import itertools
import collections
 
firstDict = {'a': 1, 'b': 2, 'both': 3}
secondDict = {'d': 100, 'e': 200, 'both': 300}

# Use the defaultdict function to return the default value for the dictionary
myDict = collections.defaultdict(int)
 
# Use the itertools.chain function takes key-value pairs to perform dictionary merge and sum the dictionary values. 
for key, val in itertools.chain(firstDict.items(), secondDict.items()):
    myDict[key] += val
     
print('Merge two dictionaries and sum the dictionary values:', dict(myDict))

Output:

Merge two dictionaries and sum the dictionary values: {'a': 1, 'b': 2, 'both': 303, 'd': 100, 'e': 200}

Summary

You should see that the above method allows you to merge two dictionaries and sum the values ​​in Python. Using the collections.Counter() function is probably the least complicated you can use this way. Thanks for reading the article. If you have any questions, please leave a comment below. I will answer as possible.

Maybe you are interested:

Leave a Reply

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