Member-only story
Searching in Numpy Array
What is Array Search?
Sometimes we need to find a specific value or check a condition inside an array.
For example:
- Find where 25 is present.
- Find all even numbers.
- Find all odd numbers.
- Find the correct position to insert a value into a sorted array.
NumPy provides different functions to perform these searching tasks.
1. np.where()
Description
np.where() is used to search an array based on a condition.
Instead of returning the values, it returns the indexes where the condition becomes True.
Syntax
np.where(condition)The condition can be:
==><>=<=%
Example 1: Find the index of a value
import numpy as nparr = np.array([12, 25, 18, 25, 30, 25])result = np.where(arr == 25)print(result)
Output
(array([1, 3, 5]),)







