How to find the product of array elements in Python
An array is a data structure that stores elements and each element is recognized by its index.
In this Answer, we will learn how to find the product of array elements in Python using the following methods:
The
math.prod()functionThe
numpy.prod()function
Using the for loop
For a given arr = [2,4,7,9,10], to find the product of the elements in the array, we need to:
Define a variable for our result and name it the
product.Assign the value
1to the variable.Iterate through the array.
Multiply the variable with the iterator,
num.Store the result in the variable
product.
The Python program is as follows:
arr = [2,4,7,9,10]product = 1# for loopfor num in arr:product = product * num# display the resultprint(product)
Explanation
Line 1: We create the variable
arr, and assign an array of 5 elements to the variable.Line 2: We create the variable
product, and assign the value1to the variable. This is done because any value multiplied with1returns the same value.Line 5: We iterate through the array.
Line 6: For each iteration, we multiply the element and store it in the variable,
product.Line 8: We use the
print()method to display the result to the console.
Using the math.prod()method
We now have a new technique to calculate the product of array elements: math.prod().
The math.prod() method is used in calculating the product of all elements present in a given iterable like tuples and lists.
Note: The list can be referred to as an array. To make use of the
math.prod()method, we must have upgraded our Python installation to version 3.8.
The Python program to find the product of array elements using the math.prod() method is given below:
# import the math libraryimport matharr = [2,4,7,9,10]product = math.prod(arr)# display the resultprint(product)
Explanation
Line 2: We import the
mathlibrary to make use of mathematical functions, constants, etc.Line 3: We assign an array to a variable,
arr.Line 4: We use
math.prod()method to calculate the product of array elements and assign it to a variable,product.Line 6: We use the
print()method to print the result to the console.
Using the numpy.prod()method
The numpy.prod() method is a method in the numpy library which is used to calculate the product of array elements of a given axis.
If the axis is negative, the product is calculated from the last to the first axis. For an axis assigned with a value of 0, the product is calculated along the column, and an axis with a value of 1, the product is calculated along the row.
The Python program to find the product of array elements using the numpy.prod() method is given below:
# import the numpy library as npimport numpy as nparr = [2,4,7,9,10]product = np.prod(arr)#display resultprint(product)
Explanation
Line 2: We import the
numpylibrary asnpto create an alias of the library. Thenumpylibrary enables high mathematical functions to be performed on arrays and matrices.Line 4: We assign an array to a variable,
arr.Line 5: We use
numpy.prod()method to calculate the product of array elements and assign it to a variable,product. By default, our axis is 0. The product of the array will be calculated along the column.Line 7: We use the
print()method to print the result to the console.
We can also find the product of a multi-dimensional array using the numpy.prod() method.
The Python program to find the product of a two-dimensional array using the numpy.prod() method is given below:
import numpy as nparr = [[4,3,2], [5,2,1]]product = np.prod(arr, axis=1)print(product)
Explanation
Line 3: We assign a two-dimensional array to a variable,
arr.Line 4: We use the
numpy.prod()method to calculate the product of array elements and assign it to a variable,product. We assign 1 to our axis, this will calculate the product along the row.Line 6: We use the
print()method to print the result to the console.