Suppose i have list of numbers [3, 51, 34]. I want to add to each element the sum of the previous elements and return a new list with these new values. So here the
[3, 51, 34]
And a trivial, procedural solution, for completeness:
a = [3,51,34] def cumsum(numbers): accum = 0 result = [] for n in numbers: accum += n result.append(accum) return result print cumsum(a)
It prints [3, 54, 88]
[3, 54, 88]