-1

In array [1,5,6], I have 3 elements and I want to multiply them like: 1 * 5 * 6

I don't want did it like that:

array[0] * array[1] * array[2]

I want it automatically no matter how big the array is.

user2390182
  • 72,016
  • 6
  • 67
  • 89
cray 1234
  • 9
  • 2
  • 1
    There's a function in `math` library. [`math.prod`](https://docs.python.org/3.10/library/math.html#math.prod) – Ch3steR Oct 27 '21 at 17:23

1 Answers1

2

You can functools.reduce your array by operator.multiplication:

from operator import mul
from functools import reduce

array = [1, 5, 6]

reduce(mul, array)
# 30
user2390182
  • 72,016
  • 6
  • 67
  • 89