How to perform element-wise multiplication of two lists?
Use a list comprehension mixed with zip()
:.
[a*b for a,b in zip(lista,listb)]
Since you're already using numpy
, it makes sense to store your data in a numpy
array rather than a list. Once you do this, you get things like element-wise products for free:
In [1]: import numpy as np
In [2]: a = np.array([1,2,3,4])
In [3]: b = np.array([2,3,4,5])
In [4]: a * b
Out[4]: array([ 2, 6, 12, 20])
Use np.multiply(a,b):
import numpy as np
a = [1,2,3,4]
b = [2,3,4,5]
np.multiply(a,b)