Python Program To Multiply all numbers in the list
In this tutorial, you will learn to multiply all numbers in a list in Python. List is an ordered set of values enclosed in square brackets [ ]. List stores some values called elements in it, which can be accessed by their particular index. We will write a function that will multiply all numbers in the list and return the product.
Multiplying all the values will give a single product. For example, for a list [2, 5, 9] the product will be 90. We will be discussing the various approaches by which we can calculate the product of all numbers in a list.
Take a look at the sample input and output
Input: list= [5, 3, 2, 7]
Output: 210
Approach
For executing this program in Python, we can follow any one of the following approaches.
- By traversing the list and multiplying each element
- Using math.prod() function
- numpy.prod() function
Let us look at all the approaches one by one
Approach 1: By using loop
In this approach, we will traverse till the end of the list to find the product. Initialize a variable product to 1 and multiply each number in the list with a product to get the result. To access each number in the list we will use for loop in Python.
Algorithm
Follow the algorithm to understand the approach better
Step 1- Define a function to multiply numbers
Step 2- Declare a variable product and set it to 1
Step 3- Run a loop for all elements in the list
Step 4- Multiply all elements to the product
Step 5- Return product
Step 7- Declare a list
Step 8- Pass list in our function
Step 9- Print value returned by the function
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach.
#multiply all numbers in list
#function
def mul_list(list) :
# Multiply elements one by one
product = 1
for i in list:
product = product * i
return product
list1 = [11, 12, 4, 3]
print(list1)
print("product: ")
print(mul_list(list1))
[11, 12, 4, 3]
product:
1584
Approach 2: Using math.prod() Function
In this approach, we have used a function prod() of the math module to calculate the product. Math module provides access to various mathematical functions like pow(), sum(), avg() in our program. Let's see the algo.
Algorithm
Follow the algorithm to understand the approach better
Step 1- Import math module in the program
Step 2- Define a function to multiply numbers
Step 3- Return math.prod(list)
Step 4- Declare list
Step 5- Call function and pass list
Step 6- Print the value returned by the function
Python Program 2
Look at the program to understand the implementation of the above-mentioned approach.
#multiply all numbers in list
#import math module
import math
def mul_list(list):
return math.prod(list)
list1 = [3, 2, 5, 4, 8, 9, 10]
print(list1)
print("product: ")
print(mul_list(list1))
[3, 2, 5, 4, 8, 9, 10]
product:
86400
Approach 3: Using numpy.prod() Function
In this approach, we have used a function prod() of the NumPy module to calculate the product. It allows a programmer to perform various advanced mathematical operations on large numbers of data.
Algorithm
Follow the algorithm to understand the approach better
Step 1- Import NumPy module in the program
Step 2- Define a function to multiply numbers
Step 3- Return numpy.prod(list)
Step 4- Declare a list
Step 5- Call function and pass the list
Step 6- Print the value returned by the function
Python Program 3
Look at the program to understand the implementation of the above-mentioned approach.
#multiply all numbers in list
#import numpy module
import numpy
def mul_list(list):
return numpy.prod(list)
list1 = [3, 2, 5, 4, 8, 9, 10]
print(list1)
print("product: ")
print(mul_list(list1))
[12, 11, 9]
product:
1188
Conclusion
In this tutorial, we have learned three ways for calculating the product of elements in a list in Python. We have used, a loop to access elements individually for calculating products. We have also seen how to use the math module and NumPy module to calculate products.