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

itertools.accumulate

Up: itertools | Next: itertools.batched
Rel: itertools.batched, itertools.chain, itertools.combinations, itertools.combinations_with_replacement
Ref: docs.python.org — itertools.accumulate
Tags: #public #python3 #stdlib #itertools


Import

import itertools
from itertools import accumulate

Signature & Overview

itertools.accumulate(iterable, func=None, *, initial=None)

Return series of accumulated sums (or other binary function results).

Usage

from itertools import accumulate
import operator

assert list(accumulate([1, 2, 3, 4])) == [1, 3, 6, 10]
assert list(accumulate([2, 3, 4], operator.mul, initial=1)) == [1, 2, 6, 24]

Gotchas

REPL Inspect

from itertools import accumulate
help(accumulate)

Linked from

Related notes