+2 votes
in Programming Languages by (11.5k points)
How can I compute the product of all elements of a list in Python?

e.g.

a=[1,2,3,4,5]

answer=10

1 Answer

0 votes
by (20k points)

You can use prod() function of Numpy to compute the product of all elements of a list.

Here is an example:

>>> import numpy
>>> import numpy as np
>>> a=[1,2,3,4,5]
>>> np.prod(a)
120

Basic approach

>>> for v in a:
...   t=t*v
...
>>> t
120

...