How to search in NumPy Arrays?
47
17-Nov-2025
Updated on 17-Nov-2025
Anubhav Kumar
17-Nov-20251.
np.where()— Find Indexes of Matching ValuesThis is the most common function used for searching.
Example: Find indexes of value 5
Output:
Indexes 1 and 3 contain value 5.
2. Search for Values Based on Condition
Example: Find all numbers greater than 6
Output:
3. Extract Elements Matching a Condition
Instead of indexes, get the elements directly:
Output:
4. Search in a 2D Array
Output:
This means row 1, column 1.
5.
np.searchsorted()— Searching Sorted ArraysUsed to find where a value should be inserted to keep an array sorted.
Output:
25 would go at index 1 to keep the array sorted.
Insert left or right position
Output:
6. Find All Non-zero Elements
Output:
7. Check If Any / All Conditions Match
Check if any element matches condition:
Output:
Check if all elements match:
Output:
Summary Table
np.where()np.nonzero()np.searchsorted()arr[condition]np.any()np.all()