Given a list, print the value obtained after multiplying all numbers in a list.
Examples:
Input : list1 = [1, 2, 3] Output : 6 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 traversaldef multiplyList(myList) : # Multiply elements one by one result = 1 for x in myList: result = result * x return result # Driver codelist1 = [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 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
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 reducelist1 = [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
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.prodimport mathlist1 = [1, 2, 3] list2 = [3, 2, 4]result1 = math.prod(list1)result2 = math.prod(list2)print(result1)print(result2) |
Output:
6 24
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Ways to split a string in different ways
- Python | Multiply Integer in Mixed List of string and numbers
- Python - Multiply all cross list element pairs
- Extending a list in Python (5 different ways)
- Different ways to clear a list in Python
- Python | Repeat and Multiply list extension
- Python - Multiply two list
- Python - Multiply Consecutive elements in list
- Multiply matrices of complex numbers using NumPy in Python
- Python - How to Multiply all items in Tuple
- Python program to multiply all the items in a dictionary
- Different ways to access Instance Variable in Python
- Reverse string in Python (5 different ways)
- Print lists in Python (4 Different Ways)
- Python | Different ways to kill a Thread
- Different ways to Invert the Binary bits in Python
- Different ways to convert a Python dictionary to a NumPy array
- Python program to multiply two matrices
- Python | Pandas Series.multiply()
- numpy.defchararray.multiply() in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

