NumPy Tutorial #11: Array Search

tech_plygrnd

TechPlygrnd

Posted on August 28, 2023

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!

💖 💪 🙅 🚩
tech_plygrnd
TechPlygrnd

Posted on August 28, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Matplotlib Tutorial #3: Plot Without Line
datascience Matplotlib Tutorial #3: Plot Without Line

September 4, 2023

Matplotlib Tutorial #2: Create a Plot
datascience Matplotlib Tutorial #2: Create a Plot

September 3, 2023

NumPy Tutorial #14: Random
datascience NumPy Tutorial #14: Random

August 31, 2023

NumPy Tutorial #11: Array Search
datascience NumPy Tutorial #11: Array Search

August 28, 2023