Input: list1 = [1, 2, 3] Output: 6 Explanation: 1 * 2 * 3 = 6 Input: list1 = [3, 2, 4] Output: 24
Method 1: workaround
Initialize the product value to 1 (not 0, if 0 multiplied by anything returns zero). Go to the end of the list, multiply each number by the product. The value stored in the product at the end will give you your final answer.
Below this Python implementation of the above approach:
Method 2: Using numpy.prod ()
Below is Python3 implementation of the above approach:
Method 3 of 3: Using a lambda function: Using numpy.array
Lambda definition not contains a return statement, it always contains the expression that is returned. We can also put a lambda definition wherever a function is expected and we don’t need to assign it to a variable at all. This is the simplicity of lambda functions. The Reduce () function in Python takes a function and a list as an argument. The function is called with a lambda function and a list, and returns the result minified by ew . This performs an iterative operation on pairs of the list.
Below is the implementation of the above approach in Python3: