How to Find the Product of a Python List

Python is well known as a platform that emphasizes versatility. It provides users with a large number of powerful data structure and container formats. What’s more, we can often perform complex functions on those elements with only a few lines of code. For example, we can even find the mathematical product of a Python list. The following Python code provides an example of how we can accomplish this using the standard Python program library.

def productList(paramList):
    value = 1
    for x in paramList:

        value = value * x
    return value
ourList = [1, 2, 3]
print(productList(ourList))

We begin this Python product of list example by creating a function, productList. We’ll use it to get the product of the list passed to it as a parameter. Next, we simply multiply the list element as we cycle through them within the for loop. We then return the value. The first line after productList is where we actually declare the list we’ll be passing to it. And finally, on the final line we print the results as well call productList with ourlist as the parameter.

Of course there’s more than one way to go about this process. NumPy in particular almost always offers up some extra options for mathematical functions in Python. Consider how easily we can find the product of a Python list comprehension with only a few lines of input code and the NumPy library.

import numpy
ourList = [1, 2, 3]
prodValue = numpy.prod(ourList)
print(prodValue)

We begin by importing the NumPy library. We continue on the next line by creating our integer list. We’ll use the same individual element parts for the list. Next, we pass this list to a numpy function called prod. As the name suggests, it will give us the product of the element passed to it. In this case we’re using a list. But the fact that it’s a NumPy method means that we can also use a numpy array with it. We could even combine it with something like the matmul function for matrix multiplication or to find a cartesian product.

In this case we’re simply using prod to find the product of our list. The value is then assigned to prodValue. Finally, we print out that value. Note that it’s the same as what we found in the first example. The biggest difference is that we were able to get this result with a single built-in function.

NumPy’s not the only library we can use to find the product of a list though. In the following example we’ll combine functools, lambda and a reduce function to find the product of a Python list.

from functools import reduce
ourList = [1, 2, 3]
prodValue = reduce((lambda x, y: x * y), ourList)
print(prodValue)

We begin by importing the reduce function from functools. We’ll be using this to pass a lambda function and our list. Reduce takes in both a function and a sequence of some form. The sequence used is our list. The function string used is lambda since it essentially gives us the ability to directly pass computational logic directly to reduce our input list element. This method is somewhat similar to how we might use Python list comprehension when defining a nested list. At the end of this assignment, we’ll now have the result of the product within our prodValue variable. All that’s left is to print it to the screen in order to verify that everything worked properly.

Finally, keep in mind that these are just a few of the ways to find the product of a list in a Python program. We can add additional libraries to get increased capabilities without much extra code. For example, if we worked with lists containing a complex number then cmath can be quite useful. Or if we needed to order by permutations then the itertools module can make things a lot easier.

How to Find the Product of a Python List

Leave a Reply

Your email address will not be published.

Scroll to top