Skip to Content
Course content

179: The functools Module: reduce

Click on the "Edit" button in the top corner of the screen to edit your slide content.

You've likely spent a lot of time writing loops to aggregate data—taking a list of things and boiling them down into a single result. Whether it's summing a total, finding a maximum, or building a complex object, the pattern is always the same: initialize a variable, loop through the collection, and update that variable. It works, but it's a bit verbose.

The Manual Accumulator Approach

Imagine you're building a system that handles configuration overrides. You have a base configuration dictionary, and then a list of "layer" dictionaries that should be merged on top of it, with each subsequent layer overriding the previous ones. The naive way to handle this is the classic accumulator loop:

def merge_configs(configs):
    final_config = {}
    for config in configs:
        final_config.update(config)
    return final_config

layers = [{'debug': False, 'port': 80}, {'port': 8080}, {'debug': True}]
print(merge_configs(layers)) # {'debug': True, 'port': 8080}

There's nothing technically "wrong" here. It's clear, it's Pythonic, and any junior dev will understand it instantly. But as you move toward a more functional style of programming, you start to notice that this pattern—taking a sequence and "folding" it into a single value—is so common that it deserves its own tool. That's where functools.reduce comes in.

Folding the Data with reduce

The reduce function takes two primary arguments: a function (which defines how to combine two elements) and an iterable. It applies that function to the first two elements, takes that result and applies the function to it and the third element, and so on, until only one value remains.

Here is how we'd rewrite that config merger using reduce:

from functools import reduce

layers = [{'debug': False, 'port': 80}, {'port': 8080}, {'debug': True}]

# We use a lambda to merge two dictionaries into one
final_config = reduce(lambda acc, curr: {**acc, **curr}, layers)

print(final_config) # {'debug': True, 'port': 8080}

I'll be honest: the first time I saw reduce, I found it confusing. But once it clicks, you realize it's just a more mathematical way of describing the loop we wrote earlier. The acc (accumulator) represents the result of all previous merges, and curr is the next item in the list we're currently processing.

Cognitive Load vs. Concise Logic

So, why bother? If the loop is easier to read, why use reduce? It comes down to a trade-off between verbosity and intent. When I see reduce in a codebase, my brain immediately signals: "This operation is collapsing a collection into a single value." I don't have to scan the body of a loop to figure out if the developer is filtering data, searching for an item, or aggregating it.

However, you have to be careful. If the combining function is complex, a lambda inside a reduce becomes a nightmare to debug. In those cases, I'd actually advise you to stick to the loop or define a named function to pass into reduce. Also, remember that Python provides specialized versions of reduce for common tasks. You should never use reduce to sum a list—just use sum(). You shouldn't use it to check if all elements are true—use all(). Use reduce when you have a custom aggregation logic that doesn't have a built-in equivalent.




📋 Practical Task

Calculating Compounded Price Multipliers

You are building a pricing engine for an e-commerce site. A product's base price is modified by a series of multipliers (e.g., a 10% discount is 0.9, a 5% luxury tax is 1.05, and a 20% seasonal sale is 0.8).

Write a script that takes a list of multipliers [0.9, 1.05, 0.8] and a base price of 100.0. Use functools.reduce to calculate the final combined multiplier first, and then apply that to the base price to get the final total. Your final result should be the base price multiplied by the reduced product of all multipliers.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.