Python:NumPy .searchsorted()

AchyutKuumar's avatar
Published Aug 5, 2025
Contribute to Docs

In NumPy, the .searchsorted() function returns the index where a value should be inserted to maintain order.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

ndarray.searchsorted(value, side='left', sorter=None)

Parameters:

  • value (array_like): Values to insert into the array.
  • side (default='left', optional): Determines whether to return the first suitable location ('left') or last ('right').
  • sorter (1-D array_like, optional): Specifies a pre-sorted index array for the search (used if the array isn’t sorted).

Return value:

The index (or indices) where values should be inserted to maintain order.

Example

In this example, .searchsorted() finds the index where the value 3 should be inserted to keep the array sorted:

import numpy as np
arr = np.array([11, 22, 33, 44, 55])
index = arr.searchsorted(33)
print(index)

The result is 2, which is the position of the first 33 in the sorted array:

2

Codebyte Example

In this codebyte example, .searchsorted() finds where a new test score should be inserted in a sorted list of scores:

Code
Output

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours