Files
30-seconds-of-code/snippets/cumsum.md
Isabelle Viktoria Maciohsek 19f636408c Make expertise a field
2022-03-01 20:27:27 +02:00

454 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
Partial sum list list intermediate 2021-01-13T23:30:41+02:00 2021-01-13T23:30:41+02:00

Creates a list of partial sums.

  • Use itertools.accumulate() to create the accumulated sum for each element.
  • Use list() to convert the result into a list.
from itertools import accumulate

def cumsum(lst):
  return list(accumulate(lst))
cumsum(range(0, 15, 3)) # [0, 3, 9, 18, 30]