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))
  1. values ← [3, 7, 2, 9, 1, 5]

    1values = [3, 7, 2, 9, 1, 5]2k = 4
    values this step[3, 7, 2, 9, 1, 5]values
  2. k ← 4

    1values = [3, 7, 2, 9, 1, 5]2k = 43mask = []
    values this step4k
  3. mask ← []

    2k = 43mask = []4for v in values:
    values this step[]mask
  4. v ← 3

    3mask = []4for v in values:5    mask.append(v > k)
    values this step3v
  5. mask ← [False]

    4for v in values:5    mask.append(v > k)6count = sum(mask)
    values this step[] [False]mask
  6. v ← 7

    3mask = []4for v in values:5    mask.append(v > k)
    values this step3 7v
  7. mask ← [False, True]

    4for v in values:5    mask.append(v > k)6count = sum(mask)
    values this step[False] [False, True]mask
  8. v ← 2

    3mask = []4for v in values:5    mask.append(v > k)
    values this step7 2v
  9. mask ← [False, True, False]

    4for v in values:5    mask.append(v > k)6count = sum(mask)
    values this step[False, True] [False, True, False]mask
  10. v ← 9

    3mask = []4for v in values:5    mask.append(v > k)
    values this step2 9v
  11. mask ← [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]mask
  12. v ← 1

    3mask = []4for v in values:5    mask.append(v > k)
    values this step9 1v
  13. mask ← [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]mask
  14. v ← 5

    3mask = []4for v in values:5    mask.append(v > k)
    values this step1 5v
  15. mask ← [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]mask
  16. for v in values:

    3mask = []4for v in values:5    mask.append(v > k)
  17. count ← 3

    5    mask.append(v > k)6count = sum(mask)7print('RESULT:', (mask, count))
    values this step3count
  18. stdout ← 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 > k is a vectorized operation that returns an array of the same shape as a with dtype=bool — one True or False per element.
  • mask.sum() works because NumPy (and Python) treat True as 1 and False as 0. This makes counting a natural extension of the sum reduction.
  • 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.