Functional Functions

functools.reduce()

Fold an iterable from left to right using a two-argument function. Without an initial value, the first item starts the fold; empty input then raises TypeError.

Formal Syntax & Structure

python syntaxpython
functools.reduce(function, iterable[, initial])

Parameters & Attributes

NameTypeDescription
functioncallableCallable applied by the operation.
iterableiterableInput values, consumed as needed.
initialobjectOptional initial accumulator; explicitly passing None uses None as that value.

Return Value

object

The object result described above.

Working Example

Example Snippetpython
import functoolsprint(functools.reduce(lambda a,b:a*b,[2,3,4]))
Python Reference