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]
A list comprehension approach, because that's always fun:
>>> data = [3, 51, 34] >>> result = [n + sum(data[:i]) for i, n in enumerate(data)] >>> result [3, 54, 88]