Back
I want to create a function that takes a list of numbers, then multiplies the numbers together.
Example:
[1,2,3,4,5,6] will give 1*2*3*4*5*6
Someone help me.
In case of Python 3, use functools.reduce
>>> from functools import reduce>>> reduce(lambda x, y: x*y, [1,2,3,4,5,6])720
>>> from functools import reduce
>>> reduce(lambda x, y: x*y, [1,2,3,4,5,6])
720
In case of Python 2, try reduce:
>>> reduce(lambda x, y: x*y, [1,2,3,4,5,6])720
try pip install six, which is compatible with python 2 and python 3, then:
>>> from six.moves import reduce>>> reduce(lambda x, y: x*y, [1,2,3,4,5,6])720
>>> from six.moves import reduce
Want to know more about python? Come and join: python course
31k questions
32.8k answers
501 comments
693 users