• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Multiply All Elements in List Using Python

How to Multiply All Elements in List Using Python

February 25, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  How to Get the Size of a List in Python Using len() Function
  • 2.  Count Number of Keys in Dictionary in Python
  • 3.  pandas mad – Calculate Mean Absolute Deviation in Python
  • 4.  Find Magnitude of Complex Number in Python
  • 5.  Using Python to Check if Deque is Empty
  • 6.  Write Integer to File Using Python
  • 7.  pi in Python – Using Math Module and Leibniz Formula to Get Value of pi
  • 8.  Using Matplotlib and Seaborn to Create Pie Chart in Python
  • 9.  Swap Two Values of Variables in Python
  • 10.  Sort by Two Keys in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy