Boolean Masks
Mask from Threshold
Compare each element of a 6-element list to a threshold in a loop, collecting
True/False values into a mask list. A second step counts how many pass. The
trace shows mask growing one boolean at a time and count assigned once
after the loop completes.
By hand
Loop over values appending v > k for each element. After the loop use the
built-in sum to count the True values (True counts as 1 in Python).
naive.py
Replay: real traced execution (multi-file project)
values = [3, 7, 2, 9, 1, 5]
k = 4
mask = []
for v in values:
mask.append(v > k)
count = sum(mask)
print('RESULT:', (mask, count))
values ← [3, 7, 2, 9, 1, 5]
1values = [3, 7, 2, 9, 1, 5]2k = 4values this step[3, 7, 2, 9, 1, 5]valuesk ← 4
1values = [3, 7, 2, 9, 1, 5]2k = 43mask = []values this step4kmask ← []
2k = 43mask = []4for v in values:values this step[]maskv ← 3
3mask = []4for v in values:5 mask.append(v > k)values this step3vmask ← [False]
4for v in values:5 mask.append(v > k)6count = sum(mask)values this step[] → [False]maskv ← 7
3mask = []4for v in values:5 mask.append(v > k)values this step3 → 7vmask ← [False, True]
4for v in values:5 mask.append(v > k)6count = sum(mask)values this step[False] → [False, True]maskv ← 2
3mask = []4for v in values:5 mask.append(v > k)values this step7 → 2vmask ← [False, True, False]
4for v in values:5 mask.append(v > k)6count = sum(mask)values this step[False, True] → [False, True, False]maskv ← 9
3mask = []4for v in values:5 mask.append(v > k)values this step2 → 9vmask ← [False, True, False, True]
4for v in values:5 mask.append(v > k)6count = sum(mask)values this step[False, True, False] → [False, True, False, True]maskv ← 1
3mask = []4for v in values:5 mask.append(v > k)values this step9 → 1vmask ← [False, True, False, True, False]
4for v in values:5 mask.append(v > k)6count = sum(mask)values this step[False, True, False, True] → [False, True, False, True, False]maskv ← 5
3mask = []4for v in values:5 mask.append(v > k)values this step1 → 5vmask ← [False, True, False, True, False, True]
4for v in values:5 mask.append(v > k)6count = sum(mask)values this step[False, True, False, True, False] → [False, True, False, True, False, True]maskfor v in values:
3mask = []4for v in values:5 mask.append(v > k)count ← 3
5 mask.append(v > k)6count = sum(mask)7print('RESULT:', (mask, count))values this step3countstdout ← RESULT: ([False, True, False, True, False, True], 3)
6count = sum(mask)7print('RESULT:', (mask, count))values this stepRESULT: ([False, True, False, True, False, True], 3)stdout
With NumPy
a > k applies the comparison to every element at once, returning a boolean
array with dtype=bool. mask.sum() counts the True values by treating each
True as 1. The snapshot shows the full mask array and the count.
library.py
import numpy as np
values = [3, 7, 2, 9, 1, 5]
k = 4
a = np.array(values)
mask = a > k
count = int(mask.sum())
print('mask: shape:', mask.shape, 'dtype:', mask.dtype, 'values:', mask.tolist())
print('count:', count)
print('RESULT:', (mask.tolist(), count))
mask: shape: (6,) dtype: bool values: [False, True, False, True, False, True]
count: 3
RESULT: ([False, True, False, True, False, True], 3)
Implementation notes
- A comparison like
a > kis a vectorized operation that returns an array of the same shape asawithdtype=bool— one True or False per element. mask.sum()works because NumPy (and Python) treatTrueas 1 andFalseas 0. This makes counting a natural extension of thesumreduction.- The mask can be used directly for boolean indexing — see
filter-with-mask. - Shape, dtype, and values are shown explicitly here because
ndarray.__repr__output varies with NumPy version and print options.