NumPy Array Search

Searching a NumPy array means locating the positions of elements that satisfy a condition, most commonly with np.where() or np.searchsorted().

Finding Elements with np.where()

np.where() is NumPy's general-purpose search tool. Given a condition, it returns the indices where that condition is True. This is far more useful than Python's built-in list search methods because the condition can be any vectorized boolean expression — greater than, less than, equal to, or a combination of several conditions.

Example

import numpy as np

arr = np.array([10, 25, 30, 45, 50, 65])

# Find indices where values are greater than 30
indices = np.where(arr > 30)
print(indices)
# (array([3, 4, 5]),)

print(arr[indices])
# [45 50 65]

np.where() as a Conditional Selector

np.where() has a second form that takes three arguments: a condition, a value to use where the condition is True, and a value to use where it is False. This turns it into a vectorized if/else, letting you build a new array in one line instead of writing a loop.

Example

import numpy as np

arr = np.array([10, 25, 30, 45, 50, 65])

# Replace values: 'high' if > 30, otherwise 'low'
labels = np.where(arr > 30, 'high', 'low')
print(labels)
# ['low' 'low' 'low' 'high' 'high' 'high']

# Common numeric use: clip negative values to zero
data = np.array([-3, 5, -1, 8, -7])
cleaned = np.where(data < 0, 0, data)
print(cleaned)
# [0 5 0 8 0]

np.searchsorted(): Binary Search on Sorted Arrays

np.searchsorted() finds the index where a value could be inserted into a sorted array while keeping it sorted. Because it relies on binary search, it runs in O(log n) time, making it dramatically faster than scanning a large sorted array with np.where(). The side parameter controls whether it returns the leftmost ('left') or rightmost ('right') valid insertion point when there are duplicate values.

Example

import numpy as np

sorted_arr = np.array([10, 20, 30, 40, 50])

# Where would 35 be inserted to keep the array sorted?
print(np.searchsorted(sorted_arr, 35))
# 3

# Search for several values at once
print(np.searchsorted(sorted_arr, [15, 25, 45]))
# [1 2 4]

# 'right' returns the index after any existing equal value
print(np.searchsorted(sorted_arr, 30, side='right'))
# 3
  • np.where() scans every element and works on arrays in any order — use it for general filtering and conditional logic.
  • np.searchsorted() assumes the array is already sorted; results are meaningless (though it won't error) if it isn't.
  • Both functions return NumPy integer arrays of indices, not the values themselves — index back into the original array to get values.
  • np.searchsorted() can take a list or array of search values and returns an insertion index for each one in a single call.
FunctionRequires Sorted InputTypical Use
np.where(condition)NoLocate all indices matching a condition
np.where(cond, a, b)NoVectorized if/else to build a new array
np.searchsorted()YesFast insertion-point lookup in sorted data
Note: If you find yourself calling np.where() repeatedly on the same large sorted array, sort it once and switch to np.searchsorted() — the speed difference becomes significant as the array grows.

Exercise: NumPy Array Search

Which function returns the indices of array elements that satisfy a given condition?