a = [1,2,3]
b = [4,5,6]
I want the output to be [5,7,9]
What is the simplest syntax that can achieve this in pure Python 3?
a = [1,2,3]
b = [4,5,6]
I want the output to be [5,7,9]
What is the simplest syntax that can achieve this in pure Python 3?
I'd say using zip()
would be a nice way
a = [1,2,3]
b = [4,5,6]
c = [x+y for x,y in zip(a,b)]
output
[5, 7, 9]
Use zip
to pair the data, then several ways
c = [x + y for x, y in zip(a, b)]
c = [sum(pair) for pair in zip(a, b)]
c = list(map(sum, zip(a, b)))