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 replay shows mask growing one boolean at a time and count assigned once after the loop completes.

By hand

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.

naive.py
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))
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.