  # Python | Multiply all the numbers in the list (3 different ways)

NumPy | Python Methods and Functions

Examples:

``` 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:      `  `     ` # Python program for multiply all values ​​in `  ` # list using traversal `      ` def ` ` multiplyList (myList): `     ` ` ` # Multiply elements one at a time `    ` result ` ` = ` ` 1 `    ` for ` ` x ` ` in ` ` myList: `  < p>  ` result ` ` = ` ` result ` ` * ` ` x `   ` return ` ` result `    ` # Driver code `   ` list1 ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 3 ` `] `  ` list2 ` ` = ` ` [` ` 3 ` `, ` ` 2 ` `, ` ` 4 ` `] `  ` print `  (multiplyList (list1))   ` print ` ` (multiplyList (list2) ) `   Output:    6 24    Method 2: Using numpy.prod ()    We can use numpy.prod ()  from import numpy to get the multiplication of all numbers in the list. Returns an integer or floating point value depending on the result of the multiplication.   Below is Python3 implementation of the above approach:      `  `     ` # Python3 multiplication program all values ​​in `  ` # list using numpy.prod () `      ` import ` ` numpy `  ` list1 ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 3 ` `] `  ` list2 ` ` = ` ` [` ` 3 ` `, ` ` 2 ` `, ` ` 4 ` `] `    ` # using numpy.prod () to get multiplications `   ` result1 ` ` = ` ` numpy.prod (list1) `  ` result2 ` ` = ` ` numpy.prod (list2) `  ` print ` ` (result1) `  ` print ` ` ( result2) `   Output:    6 24    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:           ` # Python3 program to multiply all values ​​in `  ` # list using lambda function and Reduce () `      ` from ` ` functools ` ` import ` ` reduce `   ` list1 ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 3 ` `] `  ` list2 ` ` = ` ` [` ` 3 ` ,  ` 2 ` `, ` ` 4 ` `] `        ` result1 ` ` = ` ` reduce ` ` ((` ` lambda ` ` x, y: x ` ` * ` ` y), list1) `  ` result2 ` ` = ` ` reduce ` ` ((` ` lambda ` ` x, y: x ` ` * ` ` y), list2) `  ` print ` ` (result1) `  ` print ` ` (result2) `   Exit:    6 24