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

Last updated 2 years, 10 months ago | 1145 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})