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 replay shows kept growing with each qualifying
element and staying flat when a value is rejected.
By hand
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.
naive.py
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)
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 replay:[]→[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.