Add cumsum

This commit is contained in:
Isabelle Viktoria Maciohsek
2021-01-13 23:30:41 +02:00
parent c7e1ce2c06
commit f0b50da3b6

20
snippets/cumsum.md Normal file
View File

@ -0,0 +1,20 @@
---
title: cumsum
tags: 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.
```py
from itertools import accumulate
def cumsum(lst):
return list(accumulate(lst))
```
```py
cumsum(range(0, 15, 3)) # [0, 3, 9, 18, 30]
```