Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (12.7k points)

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.

1 Answer

0 votes
by (26.4k points)

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

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

Want to know more about python? Come and join: python course

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 8, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Oct 14, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 3, 2019 in Python by Sammy (47.6k points)

Browse Categories

...