pythom sum 1

pythom sum

from collections import Counter

x = {'both1':1, 'both2':2, 'only_x': 100 }
y = {'both1':10, 'both2': 20, 'only_y':200 }

z = dict(Counter(x)+Counter(y))

print(z)
[out]:
{'both2': 22, 'only_x': 100, 'both1': 11, 'only_y': 200}

Here is what the above code is Doing:
1. Counter(x) creates a dictionary with the keys of x and the values of the count of each key.
2. Counter(y) creates a dictionary with the keys of y and the values of the count of each key.
3. dict(Counter(x)+Counter(y)) creates a dictionary with the keys of x and y and the values of the count of each key.

Similar Posts