Write a code to add the values of same keys in two different dictionaries and return a new dictionary.

Last updated 3 years, 2 months ago | 1478 views 75     5

Tags:- Python

We can use the Counter method from the collections module

from collections import Counter

dict1 = {'a': 5, 'b': 3, 'c': 2}
dict2 = {'a': 2, 'b': 4, 'c': 3}

new_dict = Counter(dict1) + Counter(dict2)


print(new_dict)
# Print: Counter({'a': 7, 'b': 7, 'c': 5})