On this page
  1. functools.reduce
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

functools.reduce

Prev: functools.partialmethod | Up: functools | Next: functools.singledispatch
Rel: functools.cache, functools.cached_property, functools.cmp_to_key, functools.lru_cache
Ref: docs.python.org — functools.reduce
Tags: #public #python3 #stdlib #functools


Import

import functools
from functools import reduce

Signature & Overview

functools.reduce(...)

Apply a function of two arguments cumulatively to the items of an iterable, from left to right.

Usage

from functools import reduce
import operator

assert reduce(operator.mul, [2, 3, 4], 1) == 24
assert reduce(lambda acc, d: acc | d, [{"a": 1}, {"b": 2}], {}) == {"a": 1, "b": 2}

Gotchas

REPL Inspect

from functools import reduce
help(reduce)

Linked from

Related notes