Takes a list of integers as input and returns the product of all of the elements in the list

def product(listIntegers): totalProduct = 1 for numbers in listIntegers: totalProduct *= numbers return totalProduct ## Using functools.reduce ## from functools import reduce def productUsingReduce(listIntegers): return reduce(lambda x, y: x*y, listIntegers)
This function takes a list of integers as input and returns the product of all of the elements in the list.

2 Responses

Try to call reduce(lambda x, y: x*y, [1, 2, 3]). Using built-in functions is more pythonic way.
@Sergey Olerinskiy Thanks for the hint Sergey!

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.