-1

I have a list like this numbers = [1, 2, 3, 4, 5]

How can I multiply all the elements of that list between themselves? Like this: 1 * 2 * 3 * 4 * 5

  • 3
    Does this answer your question? [What's the function like sum() but for multiplication? product()?](https://stackoverflow.com/questions/595374/whats-the-function-like-sum-but-for-multiplication-product) – FObersteiner Jun 21 '20 at 09:56

2 Answers2

2

Use functools.reduce (<):

>>> import functools
>>> functools.reduce(lambda x,y: x*y, [1, 2, 3, 4, 5])
120
>>> 

Use math.prod (+):

>>> import math
>>> math.prod([1, 2, 3, 4, 5], start=1)
120
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

You could do this without importing libraries like this:

 def multiplyList(myList): 
    # Multiply elements one by one 
    result = 1
    for x in myList: 
       result = result * x  
    return result  
  
numbers = [1, 2, 3] 
print(multiplyList(numbers))