Lists and Records
Count Matching
Count how many values in a list satisfy a condition — here, readings above a threshold. Incrementing a counter inside a conditional is the simplest aggregate pattern after sum and mean.
By hand
Initialise count to zero, then walk the list once. Inside the loop an if
guards the increment: count only rises when v > threshold.
naive.py
Replay: real traced execution (multi-file project)
values = [4, 7, 2, 9, 1, 8, 3, 6]
threshold = 5
count = 0
for v in values:
if v > threshold:
count = count + 1
print('RESULT:', count)
values ← [4, 7, 2, 9, 1, 8, 3, 6]
1values = [4, 7, 2, 9, 1, 8, 3, 6]2threshold = 5values this step[4, 7, 2, 9, 1, 8, 3, 6]valuesthreshold ← 5
1values = [4, 7, 2, 9, 1, 8, 3, 6]2threshold = 53count = 0values this step5thresholdcount ← 0
2threshold = 53count = 04for v in values:values this step0countv ← 4
3count = 04for v in values:5 if v > threshold:values this step4vif v > threshold:
4for v in values:5 if v > threshold:6 count = count + 1v ← 7
3count = 04for v in values:5 if v > threshold:values this step4 → 7vif v > threshold:
4for v in values:5 if v > threshold:6 count = count + 1count ← 1
5 if v > threshold:6 count = count + 17print('RESULT:', count)values this step0 → 1countv ← 2
3count = 04for v in values:5 if v > threshold:values this step7 → 2vif v > threshold:
4for v in values:5 if v > threshold:6 count = count + 1v ← 9
3count = 04for v in values:5 if v > threshold:values this step2 → 9vif v > threshold:
4for v in values:5 if v > threshold:6 count = count + 1count ← 2
5 if v > threshold:6 count = count + 17print('RESULT:', count)values this step1 → 2countv ← 1
3count = 04for v in values:5 if v > threshold:values this step9 → 1vif v > threshold:
4for v in values:5 if v > threshold:6 count = count + 1v ← 8
3count = 04for v in values:5 if v > threshold:values this step1 → 8vif v > threshold:
4for v in values:5 if v > threshold:6 count = count + 1count ← 3
5 if v > threshold:6 count = count + 17print('RESULT:', count)values this step2 → 3countv ← 3
3count = 04for v in values:5 if v > threshold:values this step8 → 3vif v > threshold:
4for v in values:5 if v > threshold:6 count = count + 1v ← 6
3count = 04for v in values:5 if v > threshold:values this step3 → 6vif v > threshold:
4for v in values:5 if v > threshold:6 count = count + 1count ← 4
5 if v > threshold:6 count = count + 17print('RESULT:', count)values this step3 → 4countfor v in values:
3count = 04for v in values:5 if v > threshold:stdout ← RESULT: 4
6 count = count + 17print('RESULT:', count)values this stepRESULT: 4stdout
The Pythonic way
sum(1 for v in values if v > threshold) is a generator expression that
produces 1 for each qualifying element; sum adds them up. No explicit
loop or counter variable is needed.
library.py
values = [4, 7, 2, 9, 1, 8, 3, 6]
threshold = 5
count = sum(1 for v in values if v > threshold)
print('RESULT:', count)
RESULT: 4
Implementation notes
- Every
if v > threshold:event is zero-delta (condition evaluation changes no variables). Matching values are distinguished by thecount = count + 1event that immediately follows; non-matching values jump straight to the next for-header. - The generator expression inside
sum(...)runs in its own scope, so its loop variable does not appear in the trace.