Boolean Masks
Filter with Mask
Keep only the values where a boolean mask is True. A zip loop pairs each value
with its mask entry; an if-check inside the loop decides whether to append. The
trace shows filtered growing only when m is True, making the selection
criterion explicit.
By hand
Pair values and mask with zip. On each iteration, if m is True append
the current v to filtered; otherwise skip.
naive.py
Replay: real traced execution (multi-file project)
values = [3, 7, 2, 9, 1, 5]
mask = [False, True, False, True, False, True]
filtered = []
for v, m in zip(values, mask):
if m:
filtered.append(v)
print('RESULT:', filtered)
values ← [3, 7, 2, 9, 1, 5]
1values = [3, 7, 2, 9, 1, 5]2mask = [False, True, False, True, False, True]values this step[3, 7, 2, 9, 1, 5]valuesmask ← [False, True, False, True, False, True]
1values = [3, 7, 2, 9, 1, 5]2mask = [False, True, False, True, False, True]3filtered = []values this step[False, True, False, True, False, True]maskfiltered ← []
2mask = [False, True, False, True, False, True]3filtered = []4for v, m in zip(values, mask):values this step[]filteredm ← False, v ← 3
3filtered = []4for v, m in zip(values, mask):5 if m:values this stepFalsem3vif m:
4for v, m in zip(values, mask):5 if m:6 filtered.append(v)m ← True, v ← 7
3filtered = []4for v, m in zip(values, mask):5 if m:values this stepFalse → Truem3 → 7vif m:
4for v, m in zip(values, mask):5 if m:6 filtered.append(v)filtered ← [7]
5 if m:6 filtered.append(v)7print('RESULT:', filtered)values this step[] → [7]filteredm ← False, v ← 2
3filtered = []4for v, m in zip(values, mask):5 if m:values this stepTrue → Falsem7 → 2vif m:
4for v, m in zip(values, mask):5 if m:6 filtered.append(v)m ← True, v ← 9
3filtered = []4for v, m in zip(values, mask):5 if m:values this stepFalse → Truem2 → 9vif m:
4for v, m in zip(values, mask):5 if m:6 filtered.append(v)filtered ← [7, 9]
5 if m:6 filtered.append(v)7print('RESULT:', filtered)values this step[7] → [7, 9]filteredm ← False, v ← 1
3filtered = []4for v, m in zip(values, mask):5 if m:values this stepTrue → Falsem9 → 1vif m:
4for v, m in zip(values, mask):5 if m:6 filtered.append(v)m ← True, v ← 5
3filtered = []4for v, m in zip(values, mask):5 if m:values this stepFalse → Truem1 → 5vif m:
4for v, m in zip(values, mask):5 if m:6 filtered.append(v)filtered ← [7, 9, 5]
5 if m:6 filtered.append(v)7print('RESULT:', filtered)values this step[7, 9] → [7, 9, 5]filteredfor v, m in zip(values, mask):
3filtered = []4for v, m in zip(values, mask):5 if m:stdout ← RESULT: [7, 9, 5]
6 filtered.append(v)7print('RESULT:', filtered)values this stepRESULT: [7, 9, 5]stdout
With NumPy
a[m] selects every element of a where the corresponding element of the
boolean array m is True. NumPy calls this boolean indexing. The snapshot
shows the input, the mask, and the filtered result with its smaller shape.
library.py
import numpy as np
values = [3, 7, 2, 9, 1, 5]
mask = [False, True, False, True, False, True]
a = np.array(values)
m = np.array(mask)
filtered = a[m]
print('a: shape:', a.shape, 'dtype:', a.dtype, 'values:', a.tolist())
print('mask: shape:', m.shape, 'dtype:', m.dtype, 'values:', m.tolist())
print('filtered: shape:', filtered.shape, 'dtype:', filtered.dtype, 'values:', filtered.tolist())
print('RESULT:', filtered.tolist())
a: shape: (6,) dtype: int64 values: [3, 7, 2, 9, 1, 5]
mask: shape: (6,) dtype: bool values: [False, True, False, True, False, True]
filtered: shape: (3,) dtype: int64 values: [7, 9, 5]
RESULT: [7, 9, 5]
Implementation notes
- Boolean indexing (
a[mask]) returns a copy, not a view. Modifying the result does not affecta. - The output shape is
(count_of_True,)— determined at runtime, not known from the input shape alone. - The mask is typically built by a comparison (
a > k) rather than written by hand — seemask-from-thresholdfor that pattern. Chaining both steps:a[a > k]selects in one expression. - For the equivalent pure-Python pattern see
filter-by-thresholdin the python-data-basics book. - Shape, dtype, and values are shown explicitly here because
ndarray.__repr__output varies with NumPy version and print options.