API · NumPy

NumPy where(): Find Indices or Select Values

Use numpy.where(condition) to find matching indices, or numpy.where(condition, x, y) to select values from x and y element by element.

On this page

numpy.where() has two forms with different results. Called as numpy.where(condition), it returns index arrays for positions where the condition is true. Called as numpy.where(condition, x, y), it chooses values from x or y element by element. Use the first form to locate matches and the second to build an array from a condition.

Answer: numpy.where() returns indices or selects values

Choose the condition-only form when you need positions, such as indices to inspect or use in another operation. Choose the three-argument form when you need a result whose values depend on a test. The condition decides which branch supplies each result element: true selects from x; false selects from y.

Syntax and parameters of numpy.where()

The call signatures are numpy.where(condition) and numpy.where(condition, x, y). The condition argument is an array-like value whose truth values determine the result. In the three-argument form, x supplies values for true positions and y supplies values for false positions. Pass x and y together; they cannot be supplied separately. Leave both out when you want indices.

For details, see the NumPy 1.18 documentation for numpy.where(). This article focuses on the two call forms and the examples below; it does not attempt to cover every array-shape combination.

Get matching indices with numpy.where(condition)

For a one-dimensional condition, the result is a one-item tuple containing an integer array. Those integers are positions in the input, not the values found at those positions. Use the tuple result directly when indexing the original array; use its first item when you specifically need the index array.

Run this example:

import numpy as np

values = np.array([4, 9, 12, 7, 15])
matching_positions = np.where(values > 10)
print(matching_positions)

This condition is true at positions 2 and 4, using Python’s zero-based indexing. The returned index array therefore identifies where the values matched; it does not itself contain 12 and 15.

Select values with numpy.where(condition, x, y)

The three-argument form returns selected values rather than positions. At each position, a true condition takes the corresponding value from x, while a false condition takes the value from y. For example, this is useful for replacing values that fail a threshold while preserving values that pass it.

Run this example:

import numpy as np

scores = np.array([42, 81, 67, 95])
labels = np.where(scores >= 70, "pass", "review")
print(labels)

Here, each score is compared with 70. The result contains "pass" for scores at or above the threshold and "review" for the others. Both alternatives are provided, as required by this call form.

Use numpy.where() with multidimensional arrays

The same selection rule applies position by position to a two-dimensional condition. Each output position is selected from the corresponding position in x or y. This makes the three-argument form useful for transforming a matrix while keeping its row and column arrangement.

Run this example:

import numpy as np

values = np.array([[10, 25, 30], [18, 50, 5]])
replacement = np.full(values.shape, -1)
selected = np.where(values > 20, values, replacement)
print(selected)

The condition tests every element. Values greater than 20 remain in the result, and the other positions take the corresponding -1 from replacement. The output retains the two-dimensional arrangement of the input.

Combine conditions with numpy.where()

To combine element-wise comparisons, put each comparison in parentheses and join them with & for “both conditions are true” or | for “at least one condition is true.” These operators work on array conditions. Python’s scalar and and or operators are not substitutes for combining element-wise array comparisons.

Run this example:

import numpy as np

values = np.array([1, 2, 3, 4, 5])
in_range = (values > 1) & (values < 5)
selected = np.where(in_range, values, 0)
print(selected)

The combined condition is true for 2, 3, and 4, so those values are kept; positions outside that range receive 0. For an either-or test, use | between parenthesized comparisons, such as (values < 2) | (values > 4). Each comparison creates an array of truth values, and numpy.where() then applies the selected branch at each position.