This article is part of in the series
Published: Friday 28th February 2025

how to use python reduce function

Python gives you a lot of in-built functions to make functional programming easier for you. One of those functions is the "reduce()". Python reduce() function is part of the "functools" module. This is used to apply a function cumulatively to a sequence, reducing it to a single result.

Today, in this guide, let us explain the reduce() function in Python in detail, its syntax, use cases, and practical examples to help you understand how to use it effectively.

What is the Python reduce() function?

The reduce() function in Python is used when you have to apply a binary function i.e., a function that takes two arguments successively to elements in an iterable, and accumulating the result. Here is how the basic syntax is structured:

from functools import reduce
reduce(function, iterable[, initializer])

Now, let us learn what each attribute does:

  • function: A function that takes two arguments and returns a single result.
  • iterable: A sequence (list, tuple, etc.) whose elements are processed.
  • initializer (optional): A starting value for the accumulation.

Here is an example where we have to do summing a list of numbers

from functools import reduce

numbers = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: x + y, numbers)
print(result) # Output: 15

In this script, "reduce()" takes the first two elements (`1 + 2 = 3`), then adds the next (`3 + 3 = 6`), and so on until the final result (15) is obtained.

How does reduce() Work

At PythonCentral, we firmly believe in teaching you how a script, module, or command works in addition to providing syntax and examples. This approach helps you apply what you've learned practically. For a given function "func(x, y)", `reduce()` applies it as like this:

result = func(func(func(func(numbers[0], numbers[1]), numbers[2]), numbers[3]), numbers[4])

For example, with `lambda x, y: x + y`, the execution flow is:

Step 1: (1 + 2) = 3
Step 2: (3 + 3) = 6
Step 3: (6 + 4) = 10
Step 4: (10 + 5) = 15
Final Output: 15

How to Use reduce() with Different Operations

Let us learn some examples where we use the Python reduce function with different operations.

How to Find the Maximum Element in a List

numbers = [3, 7, 2, 9, 5]
max_number = reduce(lambda x, y: x if x > y else y, numbers)
print(max_number)

Running this should fetch you the output "9".

How to Calculate the Product of Elements

nums = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, nums)
print(product) # Output: 120

We will know we have no errors when the output is "120".

How to Concatenate Strings in a List

words = ["Python", "Central", "Rocks"]
concatenated = reduce(lambda x, y: x + " " + y, words)
print(concatenated)

When you run this, you will get the output as "Python Central Rocks".

How to Use an Initializer in reduce()

An initializer becomes necessary when you need a starting value.

Here is an example where you have to use an Initializer for summation:

numbers = [1, 2, 3, 4, 5]
sum_with_init = reduce(lambda x, y: x + y, numbers, 10)
print(sum_with_init)

The output should give you "25".

Python reduce() Alternatives

While we agree "reduce()" is powerful, Python provides alternative, more readable methods for some cases.

  • Summing a list: Use sum(lst) as an alternative to reduce(lambda x, y: x + y, lst)
  • Finding the maximum value: Use max(lst) as an alternative to reduce(lambda x, y: x if x > y else y, lst)
  • To compute product: Use math.prod(lst) in Python 3.8 and above instead of reduce(lambda x, y: x * y, lst)

For better readability, the in-built functions like sum(), max(), and math.prod() are preferred over reduce() whenever available.

Wrapping Up

The Python reduce function is a powerful tool for functional programming in Python. It enables cumulative operations on sequences efficiently, but in many cases, alternative built-in functions can be more readable. Understanding how the Python reduce() function works and its use cases helps you to write concise, efficient, and scalable code. We at PythonCentral sincerely hope we helped you improve your functional programming skills and optimize your Python code. Get! Set! Script!

Related Articles

StackOverflow thread on this function

Python Threading for Concurrent Programming