Conditions: where, select, clip
Conditional functions in NumPy let you choose, locate, and bound array values element by element without writing a single loop — the workhorses are np.where , np.select , and np.clip .
Learn Conditions: where, select, clip in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll use np.where for if/else choices and for finding indices, np.select for many conditions, and np.clip to cap values into a range.
np.where(condition, x, y) walks through a boolean condition element by element. Where the condition is True it takes the value from x ; where it is False it takes the value from y . It's the vectorized version of an if/else.
Expected output: ['Fail' 'Pass' 'Fail' 'Pass' 'Pass'] and [2 8 6 10] (only values under 5 were doubled).
Call np.where with only the condition (no x or y) and it returns the indices where the condition is True, as a tuple of arrays. For a 1D array, use np.where(cond)[0] to get the index array.
Expected output: (array([1, 3, 4]),) , then [1 3 4] , then the values [25 40 15] .
np.where handles one if/else. For three or more cases use np.select : pass a list of conditions and a matching list of choices , plus an optional default . The first condition that is True wins for each element.
Expected output: ['A' 'B' 'C' 'F' 'F'] — 50 and 30 fall through to the default "F" .
np.clip(arr, a_min, a_max) squeezes every value into a range: anything below a_min becomes a_min , anything above a_max becomes a_max . You can also combine conditions with & (and), | (or), and ~ (not) — wrap each comparison in parentheses — then feed the mask to np.where or use it to select elements directly.
Expected output: [0 0 50 100 80] , then the one-sided clip [0 0 50 120 80] .
Expected output: the mask [False True False False False] , ['no' 'ok' 'no' 'no' 'no'] , and temps[mask] equal to [22] .
Replace each ___ so negatives become 0 and everything else is kept unchanged.
Expected output: [0 5 0 8 0] . (Answers: where , 0 .)
❌ ValueError: The truth value of an array ... is ambiguous
✅ Fix: use & and | with each comparison in parentheses, e.g. (a > 0) & (a < 10) .
Your conditions list and choices list have different lengths.
✅ Fix: give one choice per condition, and use default= for the fall-through case.
Ratings should sit between 1 and 5, but a few are out of range. Clip them into bounds, then label each "low" (≤2), "mid" (3), or "high" (≥4).
Expected output: [1 3 5 4 1] then ['low' 'mid' 'high' 'high' 'low'] .
Lesson 17 complete — you can shape data with conditions!
You used np.where to choose values and find indices, np.select for multi-way logic, and np.clip to bound values — all combined with boolean masks.
🚀 Up next: Sorting, Searching & Counting — order your data and find what's inside it.
Practice quiz
What does np.where(condition, x, y) do?
- Sorts the array
- Returns indices only
- Picks x where condition is True, else y
- Always returns x
Answer: Picks x where condition is True, else y. It chooses element-wise: x where True, y where False.
For nums = [1, 8, 3, 10], what is np.where(nums < 5, nums * 2, nums)?
Values under 5 (1 and 3) are doubled; 8 and 10 stay unchanged.
What does np.where(condition) with a single argument return?
- The indices where the condition is True
- A boolean mask
- The matching values
- A single True or False
Answer: The indices where the condition is True. With only the condition, np.where returns the indices that satisfy it.
For arr = [10, 25, 5, 40, 15], what is np.where(arr > 12)[0]?
[0] unwraps the tuple to the index array [1 3 4].
When should you use np.select instead of np.where?
- Only for 2D arrays
- When you have more than two cases
- Only for sorting
- When there is just one condition
Answer: When you have more than two cases. np.select handles many condition/choice pairs, like if/elif/else.
In np.select, what happens when no condition matches an element?
- It raises an error
- It uses the default value
- It uses the first choice
- It returns 0
Answer: It uses the default value. Unmatched elements fall through to the default argument.
What does np.clip([-5, 0, 50, 120, 80], 0, 100) return?
Below 0 becomes 0 and above 100 becomes 100.
Which operators combine boolean conditions element-wise?
- and / or
- is / not
- + / -
- & / |
Answer: & / |. Use & for and, | for or, each comparison in parentheses.
For scores = [95, 82, 67, 50, 30] with grades A/B/C and default F, what does np.select give?
- A
- A
- A
- A
- A
Answer: A. 50 and 30 miss all conditions and fall to the default 'F'.
Why can't you use Python's 'and'/'or' on NumPy arrays?
- They are too slow
- They don't work element-wise and raise a ValueError
- They reverse the array
- They only work on 1D arrays
Answer: They don't work element-wise and raise a ValueError. and/or expect a single truth value and raise an ambiguity ValueError.