Add SUM of values of two LISTS into new LIST
Default behavior in numpy.add
(numpy.subtract
, etc) is element-wise:
import numpy as np
np.add(first, second)
which outputs
array([7,9,11,13,15])
Assuming both lists a
and b
have same length, you do not need zip, numpy or anything else.
Python 2.x and 3.x:
[a[i]+b[i] for i in range(len(a))]
The zip
function is useful here, used with a list comprehension.
[x + y for x, y in zip(first, second)]
If you have a list of lists (instead of just two lists):
lists_of_lists = [[1, 2, 3], [4, 5, 6]]
[sum(x) for x in zip(*lists_of_lists)]
# -> [5, 7, 9]
From docs
import operator
list(map(operator.add, first,second))