Search a Numpy Array for a value

In a Numpy array, we can search for a specific value using the numpy.where() method. This method returns the index where the specific element is found.

Let us see an example to search an array for a value in Numpy:

import numpy as np

# Create a Numpy Array
n = np.array([10, 20, 30, 40, 50, 30])

print("Iterating array...")
for a in n:
    print(a)

# Searching a specific value 30
res = np.where(n == 30)

print("\nElement 30 found at following indexes:")
print(res)

Output

Iterating array...
10
20
30
40
50
30

Element 30 found at following indexes:
(array([2, 5], dtype=int64),)

The out above displays [2, 5] that means the value 30 found at index 2 and 5.

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.

For Videos, Join Our YouTube Channel: Join Now


Read More:

Split Numpy Array
Sorting Numpy Arrays
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment