🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to NumPy Notes
Topic #14

NumPy Array Search


Searching Arrays

You can search an array for a certain value, and return the indexes that get a match.

To search an array, use the where() method.

Example

  import numpy as np

arr = np.array([1, 2, 3, 4, 5, 4, 4])

  x =
  np.where(arr == 4)

print(x)

The example above will return a tuple: (array([3, 5, 6],)

Which means that the value 4 is present at index 3, 5, and 6.

Example

  import numpy as np

arr = np.array([10, 14, 93, 41, 8, 7])

  x =
  np.where(arr%2 == 1)

print(x)

Example

  import numpy as np

arr = np.array([10, 14, 93, 41, 8, 7])

  x =
  np.where(arr%2 == 0)

print(x)

Search Sorted

There is a method called searchsorted() which performs a binary search in the array, and returns the index where the specified value would be inserted to maintain the search order.

Note: The searchsorted() method is assumed to be used on sorted arrays.

Example

  import numpy as np

arr = np.array([6,
  7, 8, 9])

  x =
  np.searchsorted(arr, 7)

print(x)

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

The method starts the search from the left and returns the first index where the number 7 is no longer larger than the next value.

Search From the Right Side

By default the left most index is returned, but we can give side='right' to return the right most index instead.

Example

  import numpy as np

arr = np.array([6,
  7, 8, 9])

  x =
  np.searchsorted(arr, 7, side='right')

print(x)

Example explained: The number 7 should be inserted on index 2 to remain the sort order.

The method starts the search from the right and returns the first index where the number 7 is no longer less than the next value.

Multiple Values

To search for more than one value, use an array with the specified values.

Example

  import numpy as np

arr = np.array([1,
  3, 5, 7])

  x =
  np.searchsorted(arr, [2, 4, 6])

print(x)

The return value is an array: [1 2 3] containing the three indexes where 2, 4, 6 would be inserted in the original array to maintain the order.

Want to go beyond the notes?

Join CodingNow 2.0's NumPy course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

NumPy Array Search – FAQs

Quick answers about learning NumPy Array Search in NumPy.

This free note from CodingNow 2.0 explains NumPy Array Search in NumPy — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every NumPy topic on CodingNow 2.0, including NumPy Array Search, is 100% free with no signup required.
With focused practice, most students grasp NumPy Array Search in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now