GeeksforGeeks App
Open App
Browser
Continue

# Python | Multiply all numbers in the list

Given a list, print the value obtained after multiplying all numbers in a list.

Examples:

Input :  list1 = [1, 2, 3]
Output :
Explanation: 1*2*3=6

Input : list1 = [3, 2, 4]
Output : 24

Method 1: Traversal

Initialize the value of the product to 1(not 0 as 0 multiplied with anything returns zero). Traverse till the end of the list, multiply every number with the product. The value stored in the product at the end will give you your final answer.

Below is the Python implementation of the above approach:

## Python

 `# Python program to multiply all values in the``# list using traversal`  `def` `multiplyList(myList):` `    ``# Multiply elements one by one``    ``result ``=` `1``    ``for` `x ``in` `myList:``        ``result ``=` `result ``*` `x``    ``return` `result`  `# Driver code``list1 ``=` `[``1``, ``2``, ``3``]``list2 ``=` `[``3``, ``2``, ``4``]``print``(multiplyList(list1))``print``(multiplyList(list2))`

Output

```6
24```

Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(1),

Method 2: Using numpy.prod()

We can use numpy.prod() from import numpy to get the multiplication of all the numbers in the list. It returns an integer or a float value depending on the multiplication result.

Below is the Python3 implementation of the above approach:

## Python3

 `# Python3 program to multiply all values in the``# list using numpy.prod()` `import` `numpy``list1 ``=` `[``1``, ``2``, ``3``]``list2 ``=` `[``3``, ``2``, ``4``]` `# using numpy.prod() to get the multiplications``result1 ``=` `numpy.prod(list1)``result2 ``=` `numpy.prod(list2)``print``(result1)``print``(result2)`

Output:

```6
24 ```

Time complexity: O(n), where n is the length of the input lists.
Auxiliary space: O(1).

Method 3 Using lambda function: Using numpy.array

Lambda’s definition does not include a “return” statement, it always contains an expression that is returned. We can also put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all. This is the simplicity of lambda functions. The reduce() function in Python takes in a function and a list as an argument. The function is called with a lambda function and a list and a new reduced result is returned. This performs a repetitive operation over the pairs of the list.

Below is the Python3 implementation of the above approach:

## Python3

 `# Python3 program to multiply all values in the``# 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)`

Output

```6
24```

Time complexity: O(n) – where n is the length of the longer list.
Auxiliary space: O(1) – the memory used is constant, as no extra data structures are create.

Method 4 Using prod function of math library: Using math.prod

Starting Python 3.8, a prod function has been included in the math module in the standard library, thus no need to install external libraries.

Below is the Python3 implementation of the above approach:

## Python3

 `# Python3 program to multiply all values in the``# list using math.prod` `import` `math``list1 ``=` `[``1``, ``2``, ``3``]``list2 ``=` `[``3``, ``2``, ``4``]`  `result1 ``=` `math.prod(list1)``result2 ``=` `math.prod(list2)``print``(result1)``print``(result2)`

Output:

```6
24 ```

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1), as the program only uses a fixed amount of memory to store the input lists and the output variables.

Method 5: Using mul() function of operator module.

First we have to import the operator module then using the mul() function of operator module multiplying the all values in the list.

## Python3

 `# Python 3 program to multiply all numbers in``# the given list by importing operator module` `from` `operator ``import``*``list1 ``=` `[``1``, ``2``, ``3``]``m ``=` `1``for` `i ``in` `list1:``  ``# multiplying all elements in the given list``  ``# using mul function of operator module``    ``m ``=` `mul(i, m)``# printing the result``print``(m)`

Output

`6`

Time complexity:

The program contains a single for loop that iterates through each element in the input list. Therefore, the time complexity of the program is O(n), where n is the length of the input list.

Auxiliary space:

The program uses a constant amount of auxiliary space throughout its execution. Specifically, it only creates four variables: list1, m, i, and the imported mul function from the operator module. The space required to store these variables does not depend on the size of the input list, and therefore the auxiliary space complexity of the program is O(1), which is constant.

Method 6: Using traversal by index

## Python3

 `# Python program to multiply all values in the``# list using traversal` `def` `multiplyList(myList) :``    ` `    ``# Multiply elements one by one``    ``result ``=` `1``    ``for` `i ``in` `range``(``0``,``len``(myList)):``        ``result ``=` `result ``*` `myList[i]``    ``return` `result``    ` `# Driver code``list1 ``=` `[``1``, ``2``, ``3``]``list2 ``=` `[``3``, ``2``, ``4``]``print``(multiplyList(list1))``print``(multiplyList(list2))`

Output

```6
24```

Time complexity: O(n), where n is the length of the input list.

Auxiliary space: O(1), as the program uses a single variable to store the result and does not use any additional data structure.

Method 7: Using itertools.accumulate

## Python

 `# Python3 program to multiply all values in the``# list using lambda function and accumulate()` `from` `itertools ``import` `accumulate``list1 ``=` `[``1``, ``2``, ``3``]``list2 ``=` `[``3``, ``2``, ``4``]`  `result1 ``=` `list``(accumulate(list1, (``lambda` `x, y: x ``*` `y)))``result2 ``=` `list``(accumulate(list2, (``lambda` `x, y: x ``*` `y)))``print``(result1[``-``1``])``print``(result2[``-``1``])`

Output:

```6
24```

The time complexity is O(n), where n is the length of the input list.

The auxiliary space is also O(n)

Method 8: Using the recursive function

The function product_recursive() takes a list of numbers as input and returns the product of all the numbers in the list, using a recursive approach.

The steps for this function are:

1. Check if the list is empty. If it is, return 1 (the product of an empty list is defined as 1).
2. If the list is not empty, recursively call the product_recursive() function on the rest of the list (i.e., all the elements except for the first one) and multiply the result by the first element of the list.
3. Return the product obtained from step 2.

## Python3

 `def` `product_recursive(numbers):``    ``# base case: list is empty``    ``if` `not` `numbers:``        ``return` `1``    ``# recursive case: multiply first element by product of the rest of the list``    ``return` `numbers[``0``] ``*` `product_recursive(numbers[``1``:])``list1 ``=` `[``1``, ``2``, ``3``]``product ``=` `product_recursive(list1)``print``(product)  ``# Output: 6` `list2 ``=` `[``3``, ``2``, ``4``]``product ``=` `product_recursive(list2)``print``(product)  ``# Output: 24`

Output

```6
24```

The time complexity of this function is O(n), where n is the number of elements in the list. This is because the function needs to perform n recursive calls, one for each element in the list.
The space complexity is also O(n) because the function creates n recursive calls on the call stack. This means that for large lists, the function can quickly use up a lot of memory, which can cause the program to crash or slow down significantly.

## Method: Using  reduce() and the mul() function’s

One approach to solve this problem is to use the built-in reduce() function from the functools module, which can apply a function to an iterable in a cumulative way. We can use the operator.mul() function to multiply the elements together.

Here are the steps and code:

1. Import the reduce() function and mul() function from the functools and operator modules, respectively.
2. Define the input list.
3. Apply the reduce() function to the list, using mul() as the function to apply.
4. Print the result.

## Python3

 `from` `functools ``import` `reduce``from` `operator ``import` `mul` `list1 ``=` `[``1``, ``2``, ``3``]``result ``=` `reduce``(mul, list1)` `print``(result)`

Output

`6`

Time complexity: O(n)
Auxiliary space: O(1)

My Personal Notes arrow_drop_up