PyDocs

Nav

Functional Programming Modules

accumulate

itertools

function · Added in 3.2
  • accumulate(iterable[, function, *, initial=None])
Return series of accumulated sums (or other binary function results).
Changed in 3.3, 3.8
  • 3.3: Added the optional function parameter.
  • 3.8: Added the optional initial parameter.
1 Example
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list(accumulate(data, max))              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]
>>> list(accumulate(data, operator.mul))     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]

# Amortize a 5% loan of 1000 with 10 annual payments of 90
>>> update = lambda balance, payment: round(balance * 1.05) - payment
>>> list(accumulate(repeat(90, 10), update, initial=1_000))
[1000, 960, 918, 874, 828, 779, 728, 674, 618, 559, 497]
from the Python docs
See Also
No see-alsos for itertools/accumulate
0 Notes
No notes for accumulate