Answers for "combine two dictionary adding values for common keys"

0

combine two dictionary adding values for common keys

a = {
    "a": 1,
    "b": 2,
    "c": 3
}

b = {
    "a": 2,
    "b": 3,
    "d": 5
}

# Python 3.5

for key in b:
    if key in a:
        b[key] = b[key] + a[key]

c = {**a, **b}
print(c)

>>> c
{'a': 3, 'b': 5, 'c': 3, 'd': 5}
Posted by: Guest on July-30-2020

Code answers related to "combine two dictionary adding values for common keys"

Python Answers by Framework

Browse Popular Code Answers by Language