Get the index of elements in the Python loop

Create a NumPy array and iterate over the array to compare the element in the array with the given array. If the element matches print the index.

Python3




import numpy as np
 
# create numpy array elements
a = np.array([2, 3, 4, 5, 6, 45, 67, 34])
 
# display element index where value = 45
 
index_of_element = -1
for i in range(a.size):
    if a[i] == 45:
        index_of_element = i
        break
 
if index_of_element != -1:
    print("element index  : ", index_of_element)
else:
    print("The element not present")


Output:

element index  : 5

How to find the Index of value in Numpy Array ?

In this article, we are going to find the index of the elements present in a Numpy array.

Similar Reads

Using where() Method

where() method is used to specify the index of a particular element specified in the condition....

Get the index of elements in the Python loop

...

Using ndenumerate() function to find the Index of value

...

Using enumerate() function to find the Index of value

...

Contact Us