Files
30-seconds-of-code/snippets/cumsum.md
Isabelle Viktoria Maciohsek f0b50da3b6 Add cumsum
2021-01-13 23:30:41 +02:00

377 B

title, tags
title tags
cumsum list,intermediate

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]