Search code examples
pythonnumpymultiplication

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


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

Here S is an array

How will I multiply this and get the value?

SP = [53.9, 80.85, 111.72, 52.92, 126.91]

Solution

  • 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]