Back to results for “OOP”
Python › functools & Functional Programming Tools

What is the practical difference between using functools.reduce and a plain for-loop for accumulation?

Intermediate

Answer

Functionally they're equivalent, but reduce expresses the accumulation as a single expression which can be more concise for simple, well-known operations (sum, product, max), while a for-loop is usually more readable for anything involving multiple steps, side effects, or conditional logic within the accumulation. PEP 8 style favors loops or built-ins over reduce for clarity.
# reduce version
from functools import reduce
total = reduce(lambda acc, x: acc + x, nums, 0)

# loop version -- often preferred
total = 0
for x in nums:
    total += x
Real-world example A code review flags a nested reduce-of-reduce expression as hard to read and requests it be rewritten as explicit nested loops for maintainability.

Common follow-ups: Guido van Rossum's stance on reduce readability?;When does reduce genuinely improve clarity?