Files
30-seconds-of-code/snippets/cumsum.md
Isabelle Viktoria Maciohsek cbc78ee450 Bake dates into snippets
2021-06-13 19:38:10 +03:00

433 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
cumsum 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]