🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.where()

AI & DATA SCIENCE // np-where

np.where(condition, x, y) returns an array choosing elements from x where condition is True, and from y where it's False, element by element.

Syntax

np.where(condition, x, y)
np.where(condition)

Deep Dive Course

np.where(cond, x, y) is NumPy's vectorized equivalent of a conditional expression applied to every element at once: for each position, it evaluates cond and picks the corresponding element from x if True, or from y if False, broadcasting x and y to match cond's shape if they're scalars. Called with a single argument, np.where(cond), it instead returns the indices where cond is True, as a tuple of arrays, one per dimension — functionally very similar to np.argwhere(), but returned in a different tuple-of-arrays structure rather than a single 2D array of coordinate pairs.

1Understanding np.where()

np.where(cond, x, y) is NumPy's vectorized equivalent of a conditional expression applied to every element at once: for each position, it evaluates cond and picks the corresponding element from x if True, or from y if False, broadcasting x and y to match cond's shape if they're scalars. Called with a single argument, np.where(cond), it instead returns the indices where cond is True, as a tuple of arrays, one per dimension — functionally very similar to np.argwhere(), but returned in a different tuple-of-arrays structure rather than a single 2D array of coordinate pairs.

💡

np.where(cond, x, y) is the vectorized alternative to writing a Python loop with an if/else inside it — always prefer it over an explicit loop for elementwise conditional selection on NumPy arrays.

editor.html
import numpy as np

arr = np.array([1, -2, 3, -4, 5])
result = np.where(arr > 0, arr, 0)
print(result)
localhost:3000

2Practical Example

Here is a real-world application of np.where() showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.array([10, 25, 30, 15, 40])
indices = np.where(arr > 20)
print(indices)
localhost:3000

3Best Practices

Follow these guidelines when working with np.where():

1. Use np.where(cond, x, y) instead of a Python loop with if/else for elementwise conditional selection on arrays

2. Use the single-argument form, np.where(cond), when you need the indices of matching elements rather than replaced values

3. Reach for np.select() instead of np.where() when you have more than two mutually exclusive conditions/choices to combine

⚠️

Tip: np.where(cond, x, y) is the vectorized alternative to writing a Python loop with an if/else inside it — always prefer it over an explicit loop for elementwise conditional selection on NumPy arrays.

editor.html
import numpy as np

arr = np.array([1, -2, 3, -4, 5])
result = np.where(arr > 0, arr, 0)
print(result)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1, -2, 3, -4, 5])
result = np.where(arr > 0, arr, 0)
print(result)
Example 02Advanced Example
import numpy as np

arr = np.array([10, 25, 30, 15, 40])
indices = np.where(arr > 20)
print(indices)

Best Practices

  • Use np.where(cond, x, y) instead of a Python loop with if/else for elementwise conditional selection on arrays
  • Use the single-argument form, np.where(cond), when you need the indices of matching elements rather than replaced values
  • Reach for np.select() instead of np.where() when you have more than two mutually exclusive conditions/choices to combine

Interview Question

What does np.where(condition) return when called with just one argument, instead of the usual three?

Hint: Think about what information is useful when you're not choosing between two arrays.

With a single argument, np.where(condition) returns a tuple containing one array per dimension of the input, giving the coordinates of every element where condition is True — for a 1D array, that's a one-element tuple containing an array of matching indices; for a 2D array, it's a two-element tuple of row indices and column indices, paired up positionally. This is a shorthand for finding where a condition holds, functionally similar to np.argwhere(), just returned in a different structural form, a tuple of per-axis index arrays rather than a single array of coordinate pairs.

Exercises

MediumPractice using np.where() in a real scenario.
View Solution
import numpy as np

arr = np.array([1, -2, 3, -4, 5])
result = np.where(arr > 0, arr, 0)
print(result)

Frequently Asked Questions

What does np.where(condition) return when called with just one argument, instead of the usual three?

With a single argument, np.where(condition) returns a tuple containing one array per dimension of the input, giving the coordinates of every element where condition is True — for a 1D array, that's a one-element tuple containing an array of matching indices; for a 2D array, it's a two-element tuple of row indices and column indices, paired up positionally. This is a shorthand for finding where a condition holds, functionally similar to np.argwhere(), just returned in a different structural form, a tuple of per-axis index arrays rather than a single array of coordinate pairs.

Related Functions

boolean-indexingnp-argwherenp-logical-and