Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate35 min
reduce folds a list to one value

What you will learn

  • Why reduce is not a built-in in Python 3 — import functools
  • 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) vs lambda 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.

reduce from functools folds a list into one accumulator result

Import syntax:

python
import functools
result = functools.reduce(FunctionName, iterable)

Syntax breakdown

The callback always receives two arguments: m (accumulator) and p (next value).

reduce callback takes accumulator m and next value p

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.

reduce steps 10 plus 20 is 30, then 60, then 100

Process:

  1. Take the first two values from the iterable (m then p) and pass them to FunctionName. Store that result internally.
  2. Take the next unused value as the new p. The previous result becomes m. Call FunctionName again.
  3. Repeat until every element has been used.
  4. The last internal result is returned — one value.

Example: [10, 20, 30, 40] with add:

  • 10 + 20 → 30
  • 30 + 30 → 60
  • 60 + 40 → 100

Sum with lambda or def

reduce() is commonly used to calculate totals.

reduce lambda m, p sums 10 20 30 40 50 into 150
python
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.0

Lambda: lambda m, p: m + p · Output: 150.0

Lambda vs a normal function

Both implementations produce the same result.

lambda m, p and def operation both return m plus p

Named function (ReduceFunsEx2):

python
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.

reduce keeps the larger of m and p until 56 remains

Logic: lambda m, p: m if m > p else p · Final result: 56.0

python
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.0

Each 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.

reduce keeps the smaller of m and p until negative 3 remains

Logic: lambda m, p: m if m < p else p · Final result: -3.0

python
val = functools.reduce(lambda m, p: m if m < p else p, values)
print("min=", val)  # -3.0

Concatenate words

reduce() can also combine strings into a single sentence.

reduce joins Python filter map reduce with spaces into one string

Lambda: lambda m, p: m + " " + p

python
words = ["Python", "filter", "map", "reduce"]
line = functools.reduce(lambda m, p: m + " " + p, words)
# Python filter map reduce

Or a def:

python
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

reduce mistakes forgot import, one parameter, list output, missing space

Remember:

  • Import functools — there is no builtin reduce(...)
  • Callback needs two parameters (m, p), not lambda x: x + 1
  • reduce() returns one value, not a list
  • Add " " when joining words — m + p glues tokens together

Complete reduce() pipeline

This summarizes how accumulation works from start to finish.

iterable to first two m and p to accumulator then one final result

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

  1. Reduce a product: lambda m, p: m * p on [1, 2, 3, 4].
  2. Concatenate with a hyphen instead of a space.
  3. Drop as fc and call functools.reduce only.

Next: Filter → Map → Reduce pipeline.

Loading playground…

Check your understanding

reduce() quiz

How do you get reduce in Python 3?

1 / 10