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)
  1. 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]values
  2. mask ← [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]mask
  3. filtered ← []

    2mask = [False, True, False, True, False, True]3filtered = []4for v, m in zip(values, mask):
    values this step[]filtered
  4. m ← False, v ← 3

    3filtered = []4for v, m in zip(values, mask):5    if m:
    values this stepFalsem3v
  5. if m:

    4for v, m in zip(values, mask):5    if m:6        filtered.append(v)
  6. m ← True, v ← 7

    3filtered = []4for v, m in zip(values, mask):5    if m:
    values this stepFalse Truem3 7v
  7. if m:

    4for v, m in zip(values, mask):5    if m:6        filtered.append(v)
  8. filtered ← [7]

    5    if m:6        filtered.append(v)7print('RESULT:', filtered)
    values this step[] [7]filtered
  9. m ← False, v ← 2

    3filtered = []4for v, m in zip(values, mask):5    if m:
    values this stepTrue Falsem7 2v
  10. if m:

    4for v, m in zip(values, mask):5    if m:6        filtered.append(v)
  11. m ← True, v ← 9

    3filtered = []4for v, m in zip(values, mask):5    if m:
    values this stepFalse Truem2 9v
  12. if m:

    4for v, m in zip(values, mask):5    if m:6        filtered.append(v)
  13. filtered ← [7, 9]

    5    if m:6        filtered.append(v)7print('RESULT:', filtered)
    values this step[7] [7, 9]filtered
  14. m ← False, v ← 1

    3filtered = []4for v, m in zip(values, mask):5    if m:
    values this stepTrue Falsem9 1v
  15. if m:

    4for v, m in zip(values, mask):5    if m:6        filtered.append(v)
  16. m ← True, v ← 5

    3filtered = []4for v, m in zip(values, mask):5    if m:
    values this stepFalse Truem1 5v
  17. if m:

    4for v, m in zip(values, mask):5    if m:6        filtered.append(v)
  18. filtered ← [7, 9, 5]

    5    if m:6        filtered.append(v)7print('RESULT:', filtered)
    values this step[7, 9] [7, 9, 5]filtered
  19. for v, m in zip(values, mask):

    3filtered = []4for v, m in zip(values, mask):5    if m:
  20. 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 affect a.
  • 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 — see mask-from-threshold for that pattern. Chaining both steps: a[a > k] selects in one expression.
  • For the equivalent pure-Python pattern see filter-by-threshold in the python-data-basics book.
  • Shape, dtype, and values are shown explicitly here because ndarray.__repr__ output varies with NumPy version and print options.