Python reduce()
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 alambda
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 theinitializer
set, the first calculation will be between theinitializer
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 numbersnums = [15, 4, 66, 29, 34, 80]# Sum the elements in the listprint(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 numbersfoo = [5, 14, 56, 89, 4, 20]# Find the minimum value in the listprint(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:
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).
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours