Multiply all elements in a List in Python

avatar

Borislav Hadzhiev

Last updated: Jul 9, 2022

banner

Photo from Unsplash

Multiply all elements in a List in Python #

Use the math.prod() method to multiply all elements in a list, e.g. result = math.prod(my_list). The math.prod() method calculates the product of all the elements in the provided iterable.

main.py
import math from functools import reduce my_list = [2, 3, 5] # ✅ multiply all elements in list using math.prod() result = math.prod(my_list) print(result) # 👉️ 30 # --------------------------------------------------- # ✅ multiply all elements in list using reduce() result_2 = reduce(lambda x, y: x * y, my_list) print(result_2) # 👉️ 30

Make sure to import the math module at the top.

We used the math.prod method to multiply all the elements in a list.

The math.prod method calculates the product of all the elements in the provided iterable.

main.py
import math my_list = [5, 5, 5] result = math.prod(my_list) print(result) # 👉️ 125

The method takes the following 2 arguments:

NameDescription
iterableAn iterable whose elements to calculate the product of
startThe start value for the product (defaults to 1)

If the iterable is empty, the start value is returned.

Alternatively, you can use the reduce() function.

To multiply all of the elements in a list:

  1. Pass a lambda function and the list to the reduce() function.
  2. The lambda function should take the accumulator and the current value, and should return the multiplication of the two.
main.py
from functools import reduce my_list = [2, 3, 5] result_2 = reduce(lambda x, y: x * y, my_list) print(result_2) # 👉️ 30

The reduce function takes the following 3 parameters:

NameDescription
functionA function that takes 2 parameters - the accumulated value and a value from the iterable.
iterableEach element in the iterable will get passed as an argument to the function.
initializerAn optional initializer value that is placed before the items of the iterable in the calculation.
The lambda function gets called with the accumulated value and the value of the current iteration and multiplies them.

If we provide a value for the initializer argument, it is placed before the items of the iterable in the calculation.

main.py
from functools import reduce my_list = [2, 3, 5] def do_math(acc, curr): print(acc) # 👉️ is 10 on first iteration return acc * curr result_2 = reduce(do_math, my_list, 10) print(result_2) # 👉️ 300

We passed 10 for the initializer argument, so the value of the accumulator will be 10 on the first iteration.

The value of the accumulator would get set to the first element in the iterable if we didn't pass a value for the initializer.

If the iterable is empty and the initializer is provided, the initializer is returned.

If the initializer is not provided and the iterable contains only 1 item, the first item is returned.

main.py
from functools import reduce my_list = [2] result_2 = reduce(lambda acc, curr: acc * curr, my_list) print(result_2) # 👉️ 2
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.