Build a running-total list from a 6-element input by accumulating a counter inside a loop. Each step appends the current total to running. The trace shows both total and running growing together, making the running-total pattern explicit before the single-call NumPy version.

By hand

Initialize total to 0 and running to an empty list. On each iteration add v to total, then append the updated total to running.

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

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

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

    2running = []3total = 04for v in values:
    values this step0total
  4. v ← 1

    3total = 04for v in values:5    total += v
    values this step1v
  5. total ← 1

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

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

    3total = 04for v in values:5    total += v
    values this step1 2v
  8. total ← 3

    4for v in values:5    total += v6    running.append(total)
    values this step1 3total
  9. running ← [1, 3]

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

    3total = 04for v in values:5    total += v
    values this step2 3v
  11. total ← 6

    4for v in values:5    total += v6    running.append(total)
    values this step3 6total
  12. running ← [1, 3, 6]

    5    total += v6    running.append(total)7print('RESULT:', running)
    values this step[1, 3] [1, 3, 6]running
  13. v ← 4

    3total = 04for v in values:5    total += v
    values this step3 4v
  14. total ← 10

    4for v in values:5    total += v6    running.append(total)
    values this step6 10total
  15. running ← [1, 3, 6, 10]

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

    3total = 04for v in values:5    total += v
    values this step4 5v
  17. total ← 15

    4for v in values:5    total += v6    running.append(total)
    values this step10 15total
  18. running ← [1, 3, 6, 10, 15]

    5    total += v6    running.append(total)7print('RESULT:', running)
    values this step[1, 3, 6, 10] [1, 3, 6, 10, 15]running
  19. v ← 6

    3total = 04for v in values:5    total += v
    values this step5 6v
  20. total ← 21

    4for v in values:5    total += v6    running.append(total)
    values this step15 21total
  21. running ← [1, 3, 6, 10, 15, 21]

    5    total += v6    running.append(total)7print('RESULT:', running)
    values this step[1, 3, 6, 10, 15] [1, 3, 6, 10, 15, 21]running
  22. for v in values:

    3total = 04for v in values:5    total += v
  23. stdout ← RESULT: [1, 3, 6, 10, 15, 21]

    6    running.append(total)7print('RESULT:', running)
    values this stepRESULT: [1, 3, 6, 10, 15, 21]stdout

With NumPy

np.cumsum(a) computes the cumulative sum in one call, returning an array of the same length as the input where position i holds the sum of all elements up to and including index i.

library.py
import numpy as np

values = [1, 2, 3, 4, 5, 6]
a = np.array(values)
result = np.cumsum(a)
print('shape:', result.shape)
print('dtype:', result.dtype)
print('values:', result.tolist())
print('RESULT:', result.tolist())
shape: (6,)
dtype: int64
values: [1, 3, 6, 10, 15, 21]
RESULT: [1, 3, 6, 10, 15, 21]

Implementation notes

  • np.cumsum is a scan, not a reduction: the output has the same shape as the input. Every partial sum is preserved, unlike a.sum() which collapses the whole array to a single scalar.
  • Position i of the output equals a[:i+1].sum() — the sum of the first i+1 elements.
  • np.cumsum also accepts an axis argument for 2-D arrays: axis=0 produces running column sums, axis=1 produces running row sums.
  • For the equivalent pure-Python pattern see running-total in the python-data-basics book.
  • Shape, dtype, and values are shown explicitly here because ndarray.__repr__ output varies with NumPy version and print options.