In Clojure, we have a function like this
(reductions str ["foo" "bar" "quax"])
=> ["foo" "foobar" "foobarquax"]
or
(reductions + [1 2 3 4 5])
=> [1 3 6 10 15]
It's basically just reduce but it collects the intermediate results.
I'm having trouble finding an equivalent in Python. Does a base library function exist.
Python 3
You can use itertools.accumulate
from itertools import accumulate
l = [1, 2, 3, 4, 5]
print([*accumulate(l)])
Prints:
[1, 3, 6, 10, 15]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With