How to multiply individual elements of a list with a number? How to multiply individual elements of a list with a number? python python

How to multiply individual elements of a list with a number?


In NumPy it is quite simple

import numpy as npP=2.45S=[22, 33, 45.6, 21.6, 51.8]SP = P*np.array(S)

I recommend taking a look at the NumPy tutorial for an explanation of the full capabilities of NumPy's arrays:

https://scipy.github.io/old-wiki/pages/Tentative_NumPy_Tutorial


You can use built-in map function:

result = map(lambda x: x * P, S)

or list comprehensions that is a bit more pythonic:

result = [x * P for x in S]


If you use numpy.multiply

S = [22, 33, 45.6, 21.6, 51.8]P = 2.45multiply(S, P)

It gives you as a result

array([53.9 , 80.85, 111.72, 52.92, 126.91])