Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

Search Sorted

The searchsorted() method performs a binary search on the array and returns the index where the specified value should be inserted to preserve the order.

The searchsorted() method is intended for use with sorted arrays.

Example

Determine the indices where the value 7 should be inserted:

import numpy as np

arr = np.array([6789])

x = np.searchsorted(arr, 7)

print(x)

Example explained: The number 7 should be inserted at index 1 to maintain the sort order.

The method searches from the left and returns the first index where 7 is not larger than the next value.