Lesson 05
Python reduce() Deep Dive — Accumulate One Result with functools
Import functools.reduce, walk the four internal steps, then fold lists into a sum, max, min, or a single concatenated line of words.
What you will learn
- Why
reduceis not a built-in in Python 3 — importfunctools - Syntax
varname = functools.reduce(FunctionName, iterable) - The notes' four internal steps (first two values, then the next, until one result)
- Sum, max, min, and word concatenation
def operation(m, p)vslambda m, p: m + p
Functions showed a first reduce. This lesson follows reduce().txt and ReduceFun(s)Ex1–5.
What is reduce()?
reduce() combines all elements of an iterable into one final value. It is not a built-in function in Python 3, so it must be imported from the functools module.
Import syntax:
import functools
result = functools.reduce(FunctionName, iterable)Syntax breakdown
The callback always receives two arguments: m (accumulator) and p (next value).
Unlike filter() and map(), the callback takes two parameters.
Four internal steps
The teaching notes describe reduce() as repeatedly combining two values until only one result remains.
Process:
- Take the first two values from the iterable (
mthenp) and pass them toFunctionName. Store that result internally. - Take the next unused value as the new
p. The previous result becomesm. CallFunctionNameagain. - Repeat until every element has been used.
- The last internal result is returned — one value.
Example: [10, 20, 30, 40] with add:
10 + 20→3030 + 30→6060 + 40→100
Sum with lambda or def
reduce() is commonly used to calculate totals.
import functools
lst = [10.0, 20.0, 30.0, 40.0, 50.0]
res = functools.reduce(lambda m, p: m + p, lst)
print("Sum({})={}".format(lst, res)) # 150.0Lambda: lambda m, p: m + p · Output: 150.0
Lambda vs a normal function
Both implementations produce the same result.
Named function (ReduceFunsEx2):
import functools as fc
def operation(m, p):
return m + p
res = fc.reduce(operation, lst)Both callbacks always accept (m, p). That is different from filter / single-iterable map. There is no reduce(...) in the builtin namespace.
Finding maximum
Instead of using Python's max(), reduce() keeps the larger value at every step.
Logic: lambda m, p: m if m > p else p · Final result: 56.0
values = [10.0, 2.0, 12.0, 3.0, 45.0, 4.0, 56.0, 13.0, -3.0, 6.0]
val = functools.reduce(lambda m, p: m if m > p else p, values)
print("max=", val) # 56.0Each step keeps the winner and compares it to the next element.
Finding minimum
The same idea works for the smallest value by reversing the comparison.
Logic: lambda m, p: m if m < p else p · Final result: -3.0
val = functools.reduce(lambda m, p: m if m < p else p, values)
print("min=", val) # -3.0Concatenate words
reduce() can also combine strings into a single sentence.
Lambda: lambda m, p: m + " " + p
words = ["Python", "filter", "map", "reduce"]
line = functools.reduce(lambda m, p: m + " " + p, words)
# Python filter map reduceOr a def:
def concatop(m, p):
return m + " " + p
line = functools.reduce(concatop, words)Classroom code splits comma-separated input(). The playground uses a list literal.
Common mistakes
Remember:
- Import
functools— there is no builtinreduce(...) - Callback needs two parameters (
m,p), notlambda x: x + 1 reduce()returns one value, not a list- Add
" "when joining words —m + pglues tokens together
Complete reduce() pipeline
This summarizes how accumulation works from start to finish.
Interview cheat sheet
| Topic | Answer |
|---|---|
| Module | functools |
| Syntax | functools.reduce(fn, iterable) |
| Parameters | Two → m, p |
| First Call | Uses the first two elements |
| Return Value | One accumulated result |
| Applications | Sum, Max, Min, String Concatenation |
| Alias | import functools as fc |
Practice in the playground
- Reduce a product:
lambda m, p: m * pon[1, 2, 3, 4]. - Concatenate with a hyphen instead of a space.
- Drop
as fcand callfunctools.reduceonly.
Loading playground…
Check your understanding
reduce() quiz
How do you get reduce in Python 3?
1 / 10