DEV Community

TechPlygrnd
TechPlygrnd

Posted on

NumPy Tutorial #11: Array Search

In this blog, I will show you how to search a specific item in an array.


Search

NumPy has a method called where to find an item in array. This method requires an expression for its parameter. Let me show you how to implement it.

First, let us create an array

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 2])
Enter fullscreen mode Exit fullscreen mode

Then, use method where to find the index of an item in array and store it in a variable x

x = np.where(arr == 2)
Enter fullscreen mode Exit fullscreen mode

Let us print the value of x to see its output

print(x)
Enter fullscreen mode Exit fullscreen mode

The following is the output

(array([1, 5]),)
Enter fullscreen mode Exit fullscreen mode

From the output above, you can see that 2 is occurs two times in index 1 and 5.


Congratulations, that is how you can search an item in NumPy array. Thank you for reading, and have a nice day!

Top comments (0)