Filtering Arrays with Boolean Masks
Introduction
Welcome back to Boolean Arrays and Masks. We’re now on lesson 2, where we’ll put the comparison masks from the last lesson to work. A mask can do more than record which positions match: we can use it inside square brackets to collect those values. We’ll learn to filter an array with a stored or inline condition, then use a mask from one array to select matching entries from another array that lines up with it.
Index an Array with Its Own Mask
A boolean mask has one True or False value for each position in its array. When we place a same-length mask inside square brackets, NumPy keeps the values at True positions and leaves out the rest. Here, passing marks scores of at least 70, and scores[passing] selects just those scores. Unlike integer-array indexing, which you used earlier to list positions explicitly, boolean indexing selects the positions marked True. Both are forms of NumPy’s advanced indexing. As always, we import NumPy as np first; the later snippets in this lesson use the same import.
The comparison creates the mask, and the square brackets apply it to scores. The output includes the six passing scores in their original order. The score 64 is left out because its mask value is False.
Store the Mask in a Variable or Write It Inline
We can name a mask when we want to make the condition easy to read or reuse. We can also write the comparison directly inside the square brackets when we only need it once. Both styles select the same values. The stored form makes the mask itself available for another step; the inline form is a compact NumPy pattern for filtering in one expression.
The first selection applies the mask stored in passing. The second creates the mask as part of the selection. Each print displays the same passing scores, so choose the style that makes your code clearest.
Understand What Mask Filtering Returns
Mask indexing creates a new array containing the selected values; it does not remove values from the original array. The result may be shorter, because every False position is skipped. In our score example, six of the seven positions are True, so six scores appear in the result. Two edge cases are worth seeing:
When every position is True, the new array holds all seven scores; when none is True, it is an empty array, []. Either way, scores itself is unchanged.
Use a Mask from One Array to Select from Another
A mask can select from a different array when both arrays have the same length and their values line up position by position. Here, passing comes from scores, but we apply it to names. A True at a student’s position keeps both that student’s score and their name. The same idea lets us find products whose stock is 0, then select their names with a mask built from stock.
The first selection reuses the passing mask from the score example. The second builds a mask inline, keeping product names at positions where stock equals 0. The output shows the names that match each condition.
The aligned rows below show how a condition on scores selects names at the same positions.

Filter with a Computed Expression Across Arrays
As you may recall from the previous lesson, a comparison can use a computed expression. We can combine that with cross-array selection: subtract each task’s estimate from its actual time to build a mask, then use that mask to select task names. The arrays must have the same length and matching order, so each computed difference belongs to the task at the same position.
The subtraction is calculated element by element before NumPy checks which differences are greater than 10. The resulting mask then selects the matching task names. Here, coding took 15 minutes longer than estimated, and testing took 18 minutes longer.
Conclusion and Next Steps
We’ve learned to use a boolean mask in square brackets to gather the values at True positions into a new array containing the selected values, which may be shorter than the original. We can store the mask or write its condition inline, and we can use it on another array when both arrays line up in length and order. In the upcoming practices, we’ll apply these ideas to rainfall, flight delays, qualifying runners, and product margins.
