Search
 
SCRIPT & CODE EXAMPLE
 

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}
Comment

PREVIOUS NEXT
Code Example
Python :: print column in 2d numpy array 
Python :: how to remove some lines border from plt plot 
Python :: module installed but not found python 
Python :: runge kutta 
Python :: python selenium headers 
Python :: python fibonacci 
Python :: python dictionary comprehension 
Python :: move one column value down by one column in pandas 
Python :: is power of python recursion 
Python :: enumerate vs zip python same time 
Python :: how to make a numpy array 
Python :: how to use xpath with beautifulsoup 
Python :: how to play a video in tkinter window 
Python :: python list to string without brackets 
Python :: python for loop even numbers 
Python :: python send get request with headers 
Python :: append a zeros column numpy 
Python :: scanner class in python 
Python :: date object into date format python 
Python :: python iterate backwards through list 
Python :: MovieWriter stderr: ffmpeg: error while loading shared libraries: libopenh264.so.5: cannot open shared object file: No such file or directory 
Python :: django logout user 
Python :: loop through list of tuples python 
Python :: generate n different random numbers python 
Python :: how to use the print function in python 
Python :: set background colour tkinter 
Python :: install python 3.6 on centos 
Python :: reverse an array python 
Python :: python returen Thread 
Python :: change x axis frequency 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =