Finding the Product of a List in Python Made Easy

Finding the product of all elements in a list can be a useful operation for many purposes like calculating total, interest, discount etc. In this tutorial we will learn how to calculate the product of …

Updated November 21, 2023

Finding the product of all elements in a list can be a useful operation for many purposes like calculating total, interest, discount etc. In this tutorial we will learn how to calculate the product of an array or a list using python programming language.

In Python, finding the product of all elements in a list is straightforward with the use of built-in reduce() function from functools module and a lambda function. The reduce() function applies binary function passed to it to each element of sequence(list) in order so as to reduce the sequence to single output.

Here’s an example:

from functools import reduce
product_list = [1, 2, 3, 4, 5]
print(reduce((lambda x, y: x * y), product_list)) # Output: 120

In the above example, for each number in our list product_list, we are multiplying them together. The final result is printed out to be 120.

Please note that this method may not be efficient on large lists because it requires going through the entire list multiple times. To find the product of a large list, you may want to use a loop or other methods optimized for speed and memory usage.

Alternatively, you can also calculate the product using numpy:

import numpy as np
product_list = [1, 2, 3, 4, 5]
print(np.prod(product_list)) # Output: 120
Hey! Do you love Python? Want to learn more about it?
Let's connect on Twitter or LinkedIn. I talk about this stuff all the time!