How to add values from two dictionaries in Python

Adding values from two dictionaries results in a new dictionary that contains all the keys from each dictionary with the values of duplicate keys added together. For example, adding the values in {‘A’:1, ‘B’:2, ‘C’:3} with {‘B’:4, ‘C’:5, ‘D’:6} results in {‘A’: 1, ‘B’: 6, ‘C’: 8, ‘D’: 6}.

Solution for How to add values from two dictionaries in Python : You can use collections.Counter() to add values from two dictionaries Call collections.Counter(dict) with dict as the dictionary object to return a Counter object. Add the two Counter objects together and call dict(object) on the resulting object as object to convert the result into a dictionary.


how-to-add-values-from-two-dictionaries-in-python