Filtering and Transforming
Running Total
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)
values ← [2, 5, 3, 8, 1, 6, 4]
1values = [2, 5, 3, 8, 1, 6, 4]2running = 0values this step[2, 5, 3, 8, 1, 6, 4]valuesrunning ← 0
1values = [2, 5, 3, 8, 1, 6, 4]2running = 03cumulative = []values this step0runningcumulative ← []
2running = 03cumulative = []4for v in values:values this step[]cumulativev ← 2
3cumulative = []4for v in values:5 running = running + vvalues this step2vrunning ← 2
4for v in values:5 running = running + v6 cumulative.append(running)values this step0 → 2runningcumulative ← [2]
5 running = running + v6 cumulative.append(running)7print('RESULT:', cumulative)values this step[] → [2]cumulativev ← 5
3cumulative = []4for v in values:5 running = running + vvalues this step2 → 5vrunning ← 7
4for v in values:5 running = running + v6 cumulative.append(running)values this step2 → 7runningcumulative ← [2, 7]
5 running = running + v6 cumulative.append(running)7print('RESULT:', cumulative)values this step[2] → [2, 7]cumulativev ← 3
3cumulative = []4for v in values:5 running = running + vvalues this step5 → 3vrunning ← 10
4for v in values:5 running = running + v6 cumulative.append(running)values this step7 → 10runningcumulative ← [2, 7, 10]
5 running = running + v6 cumulative.append(running)7print('RESULT:', cumulative)values this step[2, 7] → [2, 7, 10]cumulativev ← 8
3cumulative = []4for v in values:5 running = running + vvalues this step3 → 8vrunning ← 18
4for v in values:5 running = running + v6 cumulative.append(running)values this step10 → 18runningcumulative ← [2, 7, 10, 18]
5 running = running + v6 cumulative.append(running)7print('RESULT:', cumulative)values this step[2, 7, 10] → [2, 7, 10, 18]cumulativev ← 1
3cumulative = []4for v in values:5 running = running + vvalues this step8 → 1vrunning ← 19
4for v in values:5 running = running + v6 cumulative.append(running)values this step18 → 19runningcumulative ← [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]cumulativev ← 6
3cumulative = []4for v in values:5 running = running + vvalues this step1 → 6vrunning ← 25
4for v in values:5 running = running + v6 cumulative.append(running)values this step19 → 25runningcumulative ← [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]cumulativev ← 4
3cumulative = []4for v in values:5 running = running + vvalues this step6 → 4vrunning ← 29
4for v in values:5 running = running + v6 cumulative.append(running)values this step25 → 29runningcumulative ← [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]cumulativefor v in values:
3cumulative = []4for v in values:5 running = running + vstdout ← 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
runningandcumulativeboth update every iteration, so no events are zero-delta except the loop-exhaustion step.itertools.accumulateaccepts an optionalfuncargument (default:operator.add); passingoperator.mulgives a running product instead.- The final value of
cumulative[-1]equalssum(values)— a useful sanity check.