NumPy Arrays
Beginner
Boolean Indexing and Filtering
Beginner
~8 min
code completion
Boolean Indexing and Filtering
One of NumPy's most powerful features is boolean indexing — selecting elements from an array using a mask of True/False values.
Instead of writing a loop:
# Slow — avoid this result = [x for x in arr if x > threshold]
NumPy lets you do it in one line:
mask = arr > threshold # boolean array result = arr[mask] # elements where mask is True
Or even more concisely:
result = arr[arr > threshold]
Your task:
Implement filter_above(arr, threshold) that returns a new 1D array containing only elements strictly greater than threshold, in their original order.
Do not use a Python loop — use boolean indexing.
Example Tests
Filter values above 3
Input: {"arr":[1,5,2,8,3],"threshold":3}
Expected: [5,8]
All elements pass
Input: {"arr":[4,7,9],"threshold":0}
Expected: [4,7,9]
No elements pass
Input: {"arr":[1,2,3],"threshold":5}
Expected: []