Python reduce()

Sriparno08's avatar
Published May 4, 2022Updated May 14, 2025
Contribute to Docs

In Python, the reduce() function returns an aggregated value that results from the application of a function to an iterable. The function is applied to the first pair of items in the iterable and the resulting value is then used with the next item in the sequence. This process repeats until item n is reached.

Syntax

The reduce() function is included in the functools module:

from functools import reduce

reduce(func_name, iterable, initializer)

Parameters:

  • func_name: The function to be applied to the iterable. It can be a lambda function or any other function defined beforehand.
  • iterable: The iterable contains the items the function will act upon. It can be a list, tuple, or string.
  • initializer: Provides the ability to specify an initial value for the first calculation. With the initializer set, the first calculation will be between the initializer and the first item in the sequence instead of the first and second items of the sequence.

Return value:

The reduce() function returns an aggregated value.

Example 1: Summing Elements in a List

This example uses the reduce() function to sum all the elements in the nums list:

from functools import reduce
# Create a list of numbers
nums = [15, 4, 66, 29, 34, 80]
# Sum the elements in the list
print(reduce(lambda x, y: x + y, nums))

Here is the output:

228

Example 2: Finding the Minimum Value in a List

This example uses the reduce() function to determine the minimum value in a list:

from functools import reduce
# Create a list of numbers
foo = [5, 14, 56, 89, 4, 20]
# Find the minimum value in the list
print(reduce(lambda x, y: x if x < y else y, foo))

Here is the output:

4

Codebyte Example: Concatenating Strings in a List

This codebyte example uses the reduce() function to concatenate the strings in a list:

Code
Output
Loading...

Frequently Asked Questions

1. Why is reduce() in the functools module and not a built-in function in Python?

Python 3 moved reduce() to functools because it’s not as universally useful or intuitive as the other functions like map() or filter().

2. Can I use a normal function instead of lambda with reduce()?

Yes. Any function that takes two arguments can be used.

3. When should I use reduce() instead of a loop?

Use reduce() when:

  • You want concise, functional-style code.
  • The operation is naturally associative and cumulative.
  • Readability is not sacrificed (i.e., the logic remains clear).

All contributors

Contribute to Docs

Learn Python on Codecademy