On this page
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
- Returns an iterator of every intermediate step: Unlike
functools.reduce()(which returns only the final scalar),accumulate()yields the running total/state at each element. initial=prepends an extra element: Withinitial=1, output length islen(iterable) + 1.
REPL Inspect
from itertools import accumulate
help(accumulate)
Linked from
- tuts/python3/stdlib_module/itertools
- tuts/python3/stdlib_module/itertools/batched
- tuts/python3/stdlib_module/itertools/chain
- tuts/python3/stdlib_module/itertools/combinations
- tuts/python3/stdlib_module/itertools/combinations_with_replacement
- tuts/python3/stdlib_module/itertools/compress
- tuts/python3/stdlib_module/itertools/count
- tuts/python3/stdlib_module/itertools/cycle
- tuts/python3/stdlib_module/itertools/dropwhile
- tuts/python3/stdlib_module/itertools/filterfalse
- tuts/python3/stdlib_module/itertools/groupby
- tuts/python3/stdlib_module/itertools/islice
- tuts/python3/stdlib_module/itertools/pairwise
- tuts/python3/stdlib_module/itertools/permutations
- tuts/python3/stdlib_module/itertools/product
- tuts/python3/stdlib_module/itertools/repeat
- tuts/python3/stdlib_module/itertools/starmap
- tuts/python3/stdlib_module/itertools/takewhile
- tuts/python3/stdlib_module/itertools/tee
- tuts/python3/stdlib_module/itertools/zip_longest
Related notes
- tuts/python3/stdlib_module/itertools/batched Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/chain Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations_with_replacement Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/itertools/compress links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/count links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/cycle links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/dropwhile links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/filterfalse links to this page; shared tags: #itertools, #stdlib