Explain Codes LogoExplain Codes Logo

How can I multiply all items in a list together with Python?

python
functions
collections
best-practices
Anton ShumikhinbyAnton Shumikhin·Feb 10, 2025
TLDR

To multiply all items in a Python list, let's exploit functools.reduce() with operator.mul:

from functools import reduce from operator import mul # Because why on earth would you do it the hard way when you can go the Python way! result = reduce(mul, [1, 2, 3, 4]) # Result is 24, as if by magic

In a nutshell, this single line of code calculates the product of the list elements.

Expanding our toolbox: multiple ways

The magic of reduce(): Reduce folds the list, much like you would fold a paper aeroplane.

# Who said playing with numbers can't be fun? result = reduce(mul, [1, 2, 3, 4, 5]) # Result is 120

Planning for emptiness: Not all lists play nice, some are empty. An initial value serves as a failsafe.

# The world isn't always what we expect, but we can plan for it! result = reduce(mul, [], 1) # Empty list? No problem, we get 1.

Shortcut with math.prod(): Python 3.8+ introduced math.prod(), it does exactly what you think.

from math import prod # Because developers love shortcuts result = prod([1, 2, 3, 4]) # Result is 24, no sweat

Classic for loop: Sometimes classic way is the best way. It's like a nice cup of Java... er... Python.

product = 1 for number in [1, 2, 3, 4]: product *= number # Looping has never been this productive [pun intended] # The product is 24

One ring to rule them all, One reduce() to bind them: Six library is the keeper of the reduce() across Python 2 and 3:

from six.moves import reduce result = reduce(mul, [1, 2, 3, 4]) # Works across Python 2 and 3, a super ring indeed

NumPy for high performance: numpy.prod() is designed for heavy-duty multiplication of large arrays:

import numpy as np result = np.prod(np.array([1, 2, 3, 4])) # This thing runs faster than the bus you missed this morning

Tackling edge cases

Positive, Negative, Zero: The methods we discussed take negative numbers and zero in stride, but remember, a single zero makes the whole product zero!

Size matters, or does it?: These methods laugh in the face of different-sized lists, they can handle whatever you throw at them.

Choose your weapon wisely: Select functools.reduce() for conciseness, math.prod() for readability, a for loop for purity of thought, and numpy.prod() for performance-centric cases.

Getting inside solutions

Understanding reduce(): The reduce() function is like a juggernaut, it rolls over the list applying the multiplication operation to accumulate the result.

The unsung hero - the initial value: An initial value does more than meet the eye, it guards against errors with empty lists and ensures correct results from reduce().

Big data, no problem: numpy.prod() thrives on large arrays with a high number of elements, by leveraging vectorized operations.

Elegance of a for loop: A for loop perfectly encapsulates the conceptual understanding of multiplication.