In Python, we can easily multiply all elements in a list. The easiest way to get the product of all items of a list is with a loop.
def multiplyNumbers(lst):
product = 1
for x in lst:
product = product * x
return product
print(multiplyNumbers([9,3,2,4])
#Output:
216
You can also use a lambda expression combined with the functools reduce() function.
from functools import reduce
list_of_numbers = [9,3,2,4]
product = reduce((lambda x,y: x*y), list_of_numbers)
print(product)
#Output:
216
If you are using numpy, you can use the numpy prod() function to multiply all elements in a list together.
import numpy as np
print(np.prod([9,3,2,4])
#Output:
216
One last way you can multiply all elements in a list together is with the math prod() function.
import math
print(math.prod([9,3,2,4])
#Output:
216
When working with lists of numbers, the ability to summarize the list and get certain statistics easily is valuable.
One such statistic is the product of all numbers in a list.
We can get the product of all numbers in a list easily in Python. TO get the product of numbers in a list, we can use a for loop and multiply each number by the cumulative product up to that point.
Below is an example of how to multiply all elements of a list together using a for loop in Python.
def multiplyNumbers(lst):
product = 1
for x in lst:
product = product * x
return product
print(multiplyNumbers([9,3,2,4])
#Output:
216
Using a Lambda Expression to Get the Product of All Elements of a List in Python
Another way we can get the product of all items in a list in Python is with a lambda expression.
We can use a lambda expression combined with the reduce() function from the functools module to multiply all numbers of a list together.
Below is an example in Python of how to use a lambda expression and reduce() to multiply all numbers in a list together.
from functools import reduce
list_of_numbers = [9,3,2,4]
product = reduce((lambda x,y: x*y), list_of_numbers)
print(product)
#Output:
216
Using Numpy to Multiply All Items of a List Together in Python
Another way you can get the product of all numbers in a list is with the numpy module.
The numpy module has a function called prod() which calculates the product of all numbers in a list.
Below is an example using numpy in Python of how to get the product of all items in a list.
import numpy as np
print(np.prod([9,3,2,4])
#Output:
216
Using math.prod() to Multiply All Elements in a List Together in Python
The Python math module has many great functions which allow us to do both easy and complex calculations.
The math module’s prod() function is the same as the numpy prod() function and enables us to easily be able to get the product of all numbers in a list.
Below is an example using the math prod() function in Python of how to multiply all items in a list together and get the product of those numbers.
import math
print(math.prod([9,3,2,4])
#Output:
216
Hopefully this article has been useful for you to learn how to multiply all the elements of a list together using Python.