Produce a cumulative-sum list where each element is the sum of all values up to that point. The trace shows running advancing with every element and cumulative growing by one entry per step.

By hand

Keep a running accumulator and append its current value to cumulative after each addition. After the loop, cumulative[i] equals the sum of the first i + 1 values.

naive.py
Replay: real traced execution (multi-file project)
values = [2, 5, 3, 8, 1, 6, 4]
running = 0
cumulative = []
for v in values:
    running = running + v
    cumulative.append(running)
print('RESULT:', cumulative)
  1. values ← [2, 5, 3, 8, 1, 6, 4]

    1values = [2, 5, 3, 8, 1, 6, 4]2running = 0
    values this step[2, 5, 3, 8, 1, 6, 4]values
  2. running ← 0

    1values = [2, 5, 3, 8, 1, 6, 4]2running = 03cumulative = []
    values this step0running
  3. cumulative ← []

    2running = 03cumulative = []4for v in values:
    values this step[]cumulative
  4. v ← 2

    3cumulative = []4for v in values:5    running = running + v
    values this step2v
  5. running ← 2

    4for v in values:5    running = running + v6    cumulative.append(running)
    values this step0 2running
  6. cumulative ← [2]

    5    running = running + v6    cumulative.append(running)7print('RESULT:', cumulative)
    values this step[] [2]cumulative
  7. v ← 5

    3cumulative = []4for v in values:5    running = running + v
    values this step2 5v
  8. running ← 7

    4for v in values:5    running = running + v6    cumulative.append(running)
    values this step2 7running
  9. cumulative ← [2, 7]

    5    running = running + v6    cumulative.append(running)7print('RESULT:', cumulative)
    values this step[2] [2, 7]cumulative
  10. v ← 3

    3cumulative = []4for v in values:5    running = running + v
    values this step5 3v
  11. running ← 10

    4for v in values:5    running = running + v6    cumulative.append(running)
    values this step7 10running
  12. cumulative ← [2, 7, 10]

    5    running = running + v6    cumulative.append(running)7print('RESULT:', cumulative)
    values this step[2, 7] [2, 7, 10]cumulative
  13. v ← 8

    3cumulative = []4for v in values:5    running = running + v
    values this step3 8v
  14. running ← 18

    4for v in values:5    running = running + v6    cumulative.append(running)
    values this step10 18running
  15. cumulative ← [2, 7, 10, 18]

    5    running = running + v6    cumulative.append(running)7print('RESULT:', cumulative)
    values this step[2, 7, 10] [2, 7, 10, 18]cumulative
  16. v ← 1

    3cumulative = []4for v in values:5    running = running + v
    values this step8 1v
  17. running ← 19

    4for v in values:5    running = running + v6    cumulative.append(running)
    values this step18 19running
  18. cumulative ← [2, 7, 10, 18, 19]

    5    running = running + v6    cumulative.append(running)7print('RESULT:', cumulative)
    values this step[2, 7, 10, 18] [2, 7, 10, 18, 19]cumulative
  19. v ← 6

    3cumulative = []4for v in values:5    running = running + v
    values this step1 6v
  20. running ← 25

    4for v in values:5    running = running + v6    cumulative.append(running)
    values this step19 25running
  21. cumulative ← [2, 7, 10, 18, 19, 25]

    5    running = running + v6    cumulative.append(running)7print('RESULT:', cumulative)
    values this step[2, 7, 10, 18, 19] [2, 7, 10, 18, 19, 25]cumulative
  22. v ← 4

    3cumulative = []4for v in values:5    running = running + v
    values this step6 4v
  23. running ← 29

    4for v in values:5    running = running + v6    cumulative.append(running)
    values this step25 29running
  24. cumulative ← [2, 7, 10, 18, 19, 25, 29]

    5    running = running + v6    cumulative.append(running)7print('RESULT:', cumulative)
    values this step[2, 7, 10, 18, 19, 25] [2, 7, 10, 18, 19, 25, 29]cumulative
  25. for v in values:

    3cumulative = []4for v in values:5    running = running + v
  26. stdout ← RESULT: [2, 7, 10, 18, 19, 25, 29]

    6    cumulative.append(running)7print('RESULT:', cumulative)
    values this stepRESULT: [2, 7, 10, 18, 19, 25, 29]stdout

The Pythonic way

itertools.accumulate(values) is a lazy iterator that yields exactly these prefix sums; wrapping with list() materialises them all at once.

library.py
import itertools
values = [2, 5, 3, 8, 1, 6, 4]
cumulative = list(itertools.accumulate(values))
print('RESULT:', cumulative)
RESULT: [2, 7, 10, 18, 19, 25, 29]

Implementation notes

  • running and cumulative both update every iteration, so no events are zero-delta except the loop-exhaustion step.
  • itertools.accumulate accepts an optional func argument (default: operator.add); passing operator.mul gives a running product instead.
  • The final value of cumulative[-1] equals sum(values) — a useful sanity check.