Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #combine #dictionary #adding #values #common #keys
ADD COMMENT
Topic
Name
8+5 =