Bird
Raised Fist0
NumPydata~20 mins

np.where() for conditional selection in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
np.where() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of np.where() with simple condition
What is the output of this code snippet?
NumPy
import numpy as np
arr = np.array([10, 15, 20, 25, 30])
result = np.where(arr > 20, arr, 0)
print(result)
A[ 0 0 0 25 30]
B[10 15 20 25 30]
C[10 15 20 0 0]
D[ 0 15 20 25 30]
Attempts:
2 left
💡 Hint
np.where(condition, x, y) returns elements from x where condition is True, else from y.
❓ data_output
intermediate
1:30remaining
Resulting array shape after np.where() with condition
Given the code below, what is the shape of the resulting array?
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
result = np.where(arr % 2 == 0, arr, -1)
print(result.shape)
A(2, 3)
B(6,)
C(3, 2)
D(3,)
Attempts:
2 left
💡 Hint
np.where returns an array with the same shape as the input when used with three arguments.
🔧 Debug
advanced
1:30remaining
Identify the error in np.where usage
What error does this code raise?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4])
result = np.where(arr > 2)
print(result)
ANo error, prints indices where condition is True
BSyntaxError: invalid syntax
CValueError: operands could not be broadcast together
DTypeError: where() missing required argument 'y' (pos 3)
Attempts:
2 left
💡 Hint
np.where can be used with one or three arguments. With one argument, it returns indices.
🚀 Application
advanced
2:00remaining
Using np.where() to replace negative values
Given the array below, which option correctly replaces all negative values with zero using np.where()?
NumPy
import numpy as np
arr = np.array([3, -1, 5, -7, 2])
Anp.where(arr > 0, arr, 0)
Bnp.where(arr < 0, 0, arr)
Cnp.where(arr <= 0, 0, arr)
Dnp.where(arr == 0, 0, arr)
Attempts:
2 left
💡 Hint
Replace values less than zero with zero, keep others unchanged.
🧠 Conceptual
expert
2:30remaining
Understanding np.where() output with multiple conditions
Consider this code snippet. What is the output of result?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
result = np.where(arr % 2 == 0, 'even', np.where(arr % 3 == 0, 'div3', 'odd'))
print(result)
A['even' 'even' 'div3' 'even' 'odd']
B['odd' 'even' 'odd' 'even' 'odd']
C['odd' 'even' 'div3' 'odd' 'odd']
D['odd' 'even' 'div3' 'even' 'odd']
Attempts:
2 left
💡 Hint
np.where can be nested to check multiple conditions in order.

Practice

(1/5)
1. What does the np.where() function do in NumPy?
easy
A. Selects elements from two arrays based on a condition
B. Sorts an array in ascending order
C. Calculates the mean of an array
D. Creates a new array filled with zeros

Solution

  1. Step 1: Understand the purpose of np.where()

    np.where() is used to choose elements from two arrays depending on a condition.
  2. Step 2: Compare with other options

    Sorting, calculating mean, or creating zeros are different functions like np.sort(), np.mean(), and np.zeros().
  3. Final Answer:

    Selects elements from two arrays based on a condition -> Option A
  4. Quick Check:

    np.where() = conditional selection [OK]
Hint: np.where picks from two arrays based on a condition [OK]
Common Mistakes:
  • Confusing np.where() with sorting or aggregation functions
  • Thinking np.where() creates new arrays without conditions
  • Assuming np.where() only finds indices, not selects values
2. Which of the following is the correct syntax to use np.where() to select elements from arrays a and b based on condition cond?
easy
A. np.where(a, cond, b)
B. np.where(cond, a, b)
C. np.where(a, b, cond)
D. np.where(cond, b, a)

Solution

  1. Step 1: Recall np.where() parameter order

    The correct order is np.where(condition, x, y) where elements from x are chosen if condition is True, else from y.
  2. Step 2: Match parameters with options

    np.where(cond, a, b) matches this order: np.where(cond, a, b). Others mix the order incorrectly.
  3. Final Answer:

    np.where(cond, a, b) -> Option B
  4. Quick Check:

    np.where(condition, x, y) correct syntax [OK]
Hint: Remember: condition first, then true array, then false array [OK]
Common Mistakes:
  • Swapping the order of arrays and condition
  • Putting arrays before the condition
  • Using np.where() without parentheses
3. What is the output of the following code?
import numpy as np
arr = np.array([10, 20, 30, 40])
result = np.where(arr > 25, arr, 0)
print(result)
medium
A. [ 0 0 30 40]
B. [10 20 30 40]
C. [10 20 0 0]
D. [30 40 0 0]

Solution

  1. Step 1: Evaluate the condition arr > 25

    Elements greater than 25 are 30 and 40 at indices 2 and 3.
  2. Step 2: Apply np.where() selection

    For elements > 25, keep original value; else replace with 0. So result is [0, 0, 30, 40].
  3. Final Answer:

    [ 0 0 30 40] -> Option A
  4. Quick Check:

    Condition true keeps value, false replaces with 0 [OK]
Hint: Check condition element-wise, replace false with second array [OK]
Common Mistakes:
  • Confusing which elements satisfy the condition
  • Mixing up true and false array values
  • Forgetting np.where returns a new array
4. Identify the error in this code snippet:
import numpy as np
x = np.array([1, 2, 3, 4])
y = np.array([10, 20, 30, 40])
result = np.where(x > 2, y)
print(result)
medium
A. Condition x > 2 is invalid syntax
B. Arrays x and y have different lengths
C. Missing third argument for false condition in np.where()
D. np.where() cannot be used with arrays

Solution

  1. Step 1: Check np.where() arguments

    np.where() requires three arguments for conditional selection: condition, array if True, array if False.
  2. Step 2: Identify missing argument

    The code only provides condition and one array, missing the third argument for false case.
  3. Final Answer:

    Missing third argument for false condition in np.where() -> Option C
  4. Quick Check:

    np.where needs condition, true array, false array [OK]
Hint: np.where needs three arguments for selection [OK]
Common Mistakes:
  • Using np.where with only two arguments for selection
  • Assuming np.where can infer missing arrays
  • Confusing np.where with np.nonzero
5. Given arrays a = np.array([5, 15, 25, 35]) and b = np.array([100, 200, 300, 400]), how can you create a new array that takes elements from b when a is greater than 20, otherwise from a, using np.where()?
hard
A. np.where(a > 20, a, b)
B. np.where(b > 20, a, b)
C. np.where(a < 20, b, a)
D. np.where(a > 20, b, a)

Solution

  1. Step 1: Understand the condition and selection

    We want elements from b where a > 20, else from a.
  2. Step 2: Apply np.where() with correct order

    Use np.where(condition, x, y) where x is chosen if condition True, else y. So np.where(a > 20, b, a).
  3. Final Answer:

    np.where(a > 20, b, a) -> Option D
  4. Quick Check:

    Condition true picks b, false picks a [OK]
Hint: Condition first, then true array, then false array [OK]
Common Mistakes:
  • Swapping arrays in np.where() parameters
  • Using wrong comparison operator
  • Confusing which array to pick on true condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard