.where()

Anonymous contributor's avatar
Anonymous contributor
Published May 11, 2025
Contribute to Docs

The .where() function is a built-in method in NumPy used for conditional selection of elements in arrays. It returns elements based on a condition:

  • When only the condition is provided, .where() returns the indices where the condition is True.
  • When all three arguments are provided i.e. condition, x, and y, it returns an array where elements from x are selected where the condition is True, and elements from y are selected where the condition is False.

Syntax

numpy.where(condition[, x, y])

Parameters:

  • condition: A boolean array or condition expression. Elements that evaluate to True will take values from x, and elements that evaluate to False will take values from y.
  • x (optional): The array or value to be selected when the condition is True.
  • y (optional): The array or value to be selected when the condition is False.

Return value:

  • When all three arguments are provided, it returns an array with elements taken from x where the condition is True, and elements taken from y where the condition is False.
  • When only the condition is provided, it returns the indices where the condition is True.

Example

The following example demonstrates how to use the .where() function with the parameters:

import numpy as np
arr = np.array([10, 15, 20, 25, 30])
# Get the indices where elements are greater than 20
indices = np.where(arr > 20)
print(indices)
# Use condition to select values from two arrays
result = np.where(arr > 20, "big", "small")
print(result)

This example results in the following output:

(array([3, 4]),)
['small' 'small' 'small' 'big' 'big']

Codebyte example

Run the following codebyte example better to understand the .where() function:

Code
Output
Loading...

In this example, elements less than 3 are replaced with 0, and others with 1.

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy