- Learning Objectives
- Prerequisites
- Introduction
- What is reduce()?
- Why is reduce() Required?
- Syntax
- Parameters
- Return Value
- reduce() vs map() vs filter()
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Understand the reduce() function.
- Use reduce() with lambda functions.
- Use reduce() with normal functions.
- Perform cumulative calculations.
- Apply reduce() to real-world problems.
- Differentiate map(), filter(), and reduce().
Before learning this chapter, you should know:
- Functions
- Lambda Functions
- map()
- filter()
- Lists
- Operators
Sometimes we need a single result from an entire collection.
Examples include:
- Sum of all numbers.
- Product of all numbers.
- Largest value.
- Smallest value.
- Concatenating strings.
Instead of writing loops,
Python provides the
reduce()function.
Unlike
map()and
filter()the
reduce()function returns only one final value.
The reduce() function repeatedly applies a function to the elements of an iterable until a single cumulative result is produced.
reduce()combines all elements of an iterable into a single value.
Suppose we have
[10,20,30,40]
If we need
100
instead of writing a loop,
we can simply use
reduce()The reduce() function belongs to
functoolsmodule.
from functools import reducereduce(function, iterable)reduce(function, iterable, initial_value)A function that accepts two arguments and returns one value.
The returned value becomes the first argument for the next iteration.
Any iterable object.
Examples
- List
- Tuple
- Set
An initial value supplied before processing the iterable.
Returns
A single cumulative value
Example
numbers = [10,20,30,40]Execution
10 + 20 = 30
30 + 30 = 60
60 + 40 = 100
Final Result
100
| map() | filter() | reduce() |
|---|---|---|
| Transforms values | Filters values | Combines values |
| Returns iterable | Returns iterable | Returns one value |
| Same number of elements | Fewer or equal elements | Single result |
Iterable
↓
First Two Elements
↓
Function Applied
↓
Result
↓
Next Element
↓
Function Applied
↓
Final Value
↓
Output
- Less code.
- Easy cumulative calculations.
- Works well with lambda.
- Improves readability.
- Useful in functional programming.
- Use lambda for simple operations.
- Use normal functions for complex logic.
- Import reduce from functools.
- Use reduce only when a single cumulative value is required.
- Avoid overly complex lambda expressions.
Using reduce() without importing it.
Incorrect
reduce(...)Correct
from functools import reducePassing a function with only one parameter.
The function must accept two parameters.
Using reduce() when map() or filter() is more appropriate.
Remember:
- map() → Transform
- filter() → Select
- reduce() → Combine
from functools import reduce
numbers = [10,20,30,40]
result = reduce(lambda x,y:x+y,numbers)
print(result)100
from functools import reduce
numbers=[2,3,4,5]
result=reduce(lambda x,y:x*y,numbers)
print(result)120
from functools import reduce
numbers=[12,45,8,67,25]
result=reduce(lambda x,y:x if x>y else y,numbers)
print(result)67
from functools import reduce
numbers=[12,45,8,67,25]
result=reduce(lambda x,y:x if x<y else y,numbers)
print(result)8
from functools import reduce
words=["Python","Java","SQL"]
result=reduce(lambda x,y:x+" "+y,words)
print(result)Python Java SQL
from functools import reduce
def add(a,b):
return a+b
numbers=[5,10,15]
print(reduce(add,numbers))30
from functools import reduce
numbers=[1,2,3]
result=reduce(lambda x,y:x+y,numbers,10)
print(result)16
Initial value = 10
10 + 1 = 11
11 + 2 = 13
13 + 3 = 16
from functools import reduce
numbers=[2,3,4]
print(reduce(lambda x,y:x*y,numbers,2))48
from functools import reduce
names=["Java","Python","Oracle","SQL"]
result=reduce(
lambda x,y:x if len(x)>len(y) else y,
names
)
print(result)Python
from functools import reduce
words=["AI","ML","Python"]
result=reduce(
lambda x,y:x+len(y),
words[1:],
len(words[0])
)
print(result)10
List
↓
reduce()
↓
First Two Elements
↓
Operation
↓
Result
↓
Next Element
↓
Operation
↓
Final Result
↓
Output
numbers
│
├──10
├──20
├──30
└──40
↓
reduce()
↓
Lambda Function
↓
Accumulator
↓
100
- Produces a single result.
- Reduces manual looping.
- Excellent for cumulative calculations.
- Supports lambda and normal functions.
- Makes aggregation concise.
- Import from
functools. - Use descriptive lambda expressions.
- Prefer normal functions for complex logic.
- Use initial values when appropriate.
- Choose reduce() only for aggregation tasks.
- Forgetting to import
reduce. - Passing a one-parameter function.
- Using reduce() where map() or filter() is more suitable.
- Writing unreadable lambda expressions.
reduce() combines all elements of an iterable into a single cumulative value.
functoolsA single value.
Two arguments.
Yes.
Lambda functions are commonly used with reduce().
Yes.
The initial value is processed before the iterable elements.
map()transforms values.filter()selects values.reduce()combines values into one result.
When a single cumulative result is required, such as calculating a total, product, maximum, or minimum.
- Find the sum of numbers.
- Find the product of numbers.
- Find the maximum value.
- Find the minimum value.
- Concatenate strings using reduce().
reduce()→ Combines values.- Returns one value.
- Import from
functools. - Uses two-parameter function.
- Supports initial value.
- Best for aggregation.
In this chapter, you learned:
- reduce() Function
- Syntax
- Parameters
- Initial Value
- Lambda with reduce()
- Normal Function with reduce()
- Practical Programs
- reduce() vs map() vs filter()
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision