Filtering and Transforming
Filter by Threshold
Walk a list and keep only the values that meet a condition — here, readings
at or above a threshold. The trace shows kept growing with each qualifying
element and staying flat when a value is rejected.
By hand
Initialise an empty kept list. Inside the loop an if guards the append:
kept only grows when v >= threshold.
naive.py
Replay: real traced execution (multi-file project)
values = [3, 7, 1, 9, 4, 8, 2, 6]
threshold = 5
kept = []
for v in values:
if v >= threshold:
kept.append(v)
print('RESULT:', kept)
values ← [3, 7, 1, 9, 4, 8, 2, 6]
1values = [3, 7, 1, 9, 4, 8, 2, 6]2threshold = 5values this step[3, 7, 1, 9, 4, 8, 2, 6]valuesthreshold ← 5
1values = [3, 7, 1, 9, 4, 8, 2, 6]2threshold = 53kept = []values this step5thresholdkept ← []
2threshold = 53kept = []4for v in values:values this step[]keptv ← 3
3kept = []4for v in values:5 if v >= threshold:values this step3vif v >= threshold:
4for v in values:5 if v >= threshold:6 kept.append(v)v ← 7
3kept = []4for v in values:5 if v >= threshold:values this step3 → 7vif v >= threshold:
4for v in values:5 if v >= threshold:6 kept.append(v)kept ← [7]
5 if v >= threshold:6 kept.append(v)7print('RESULT:', kept)values this step[] → [7]keptv ← 1
3kept = []4for v in values:5 if v >= threshold:values this step7 → 1vif v >= threshold:
4for v in values:5 if v >= threshold:6 kept.append(v)v ← 9
3kept = []4for v in values:5 if v >= threshold:values this step1 → 9vif v >= threshold:
4for v in values:5 if v >= threshold:6 kept.append(v)kept ← [7, 9]
5 if v >= threshold:6 kept.append(v)7print('RESULT:', kept)values this step[7] → [7, 9]keptv ← 4
3kept = []4for v in values:5 if v >= threshold:values this step9 → 4vif v >= threshold:
4for v in values:5 if v >= threshold:6 kept.append(v)v ← 8
3kept = []4for v in values:5 if v >= threshold:values this step4 → 8vif v >= threshold:
4for v in values:5 if v >= threshold:6 kept.append(v)kept ← [7, 9, 8]
5 if v >= threshold:6 kept.append(v)7print('RESULT:', kept)values this step[7, 9] → [7, 9, 8]keptv ← 2
3kept = []4for v in values:5 if v >= threshold:values this step8 → 2vif v >= threshold:
4for v in values:5 if v >= threshold:6 kept.append(v)v ← 6
3kept = []4for v in values:5 if v >= threshold:values this step2 → 6vif v >= threshold:
4for v in values:5 if v >= threshold:6 kept.append(v)kept ← [7, 9, 8, 6]
5 if v >= threshold:6 kept.append(v)7print('RESULT:', kept)values this step[7, 9, 8] → [7, 9, 8, 6]keptfor v in values:
3kept = []4for v in values:5 if v >= threshold:stdout ← RESULT: [7, 9, 8, 6]
6 kept.append(v)7print('RESULT:', kept)values this stepRESULT: [7, 9, 8, 6]stdout
The Pythonic way
A list comprehension [v for v in values if v >= threshold] expresses the
same filter in one line, without an explicit accumulator or append call.
library.py
values = [3, 7, 1, 9, 4, 8, 2, 6]
threshold = 5
kept = [v for v in values if v >= threshold]
print('RESULT:', kept)
RESULT: [7, 9, 8, 6]
Implementation notes
keptgrows visibly in the trace:[]→[7]→[7, 9]→[7, 9, 8]→[7, 9, 8, 6]; the four rejected values produce zero-delta events.- Every
if v >= threshold:event is zero-delta (condition evaluation changes no variables); thekept.append(v)event that follows marks a passing value. - The list comprehension runs its own internal loop, so
vdoes not appear as a traced variable in the library half.