Find index of element in array Python
In this post, you will learn how to find the index of element in an array using the Python programming language.
An array is a container that can hold a fixed number of items, and these items should be of the same type. NumPy is used to work with arrays. Python provides different ways to find the index of element in array. Here, we have mentioned most of them.
Python find index of element in array using where() method
The numpy.where() function returns the indices of elements in an array where the given condition is satisfied.
Syntax-numpy.where(condition[, x, y])
Example-
# import numpy package
import numpy as np
# create an numpy array
arr = np.array([2, 5, 6, 3, 9, 8, 2, 2, 5])
print("All index value of 2 is: ", np.where(arr == 2)[0])
print("First index value of 5 is: ",np.where(arr == 5)[0][0])
Output of the above code:
All index value of 2 is: [0 6 7]
First index value of 5 is: 1
Python find index of element in array using forloop
In the given Python program, we have used the forloop to find the index of element in an array.
# import numpy package
import numpy as np
# create an numpy array
arr = np.array([2, 5, 6, 3, 9, 8, 2, 2, 5])
index = -1
for i in range(arr.size):
if arr[i] == 8:
index = i
break
if index != -1:
print("The index of element : ", index)
else:
print("The element not present")
Output of the above code:
The index of element : 5
Python find index of element in array using enumerate() function
The enumerate() is a predefined function in Python that is used to add a counter to an iteration and returns it in the form of an enumerate object. Here, we have used this function to find the index of element in an array.
# import numpy package
import numpy as np
# create an numpy array
arr = np.array([2, 5, 6, 3, 9, 8, 2, 2, 5])
print(next(i for i, x in
enumerate(arr) if x == 9))
Output of the above code:
(1,)
Python find index of element in array using ndenumerate() function
In the given Python program, we have used the ndenumerate() function to find the index of element in an array.
# import numpy package
import numpy as np
def findIndex(arr, element):
for index, val in np.ndenumerate(arr):
if val == element:
return index
# create an numpy array
arr = np.array([2, 5, 6, 3, 9, 8, 2, 2, 5])
print(findIndex(arr,5))
Output of the above code:
(1,)
Related Articles
Python program to print all even numbers between 1 to 100
Pandas DataFrame to JSON
Prettytable in Python
Count consonants in a string Python
Python split strings by comma
Count vowels in a string Python
Replace multiple characters Python
Convert array to list Python
Python loop through list
Difference between tuple and list in Python
Convert string to list Python
Remove last element from list Python
Convert list to string Python
Convert Python list to numpy array
Remove element from list Python
Python dict inside list
Check if list is empty Python
Python iterate list with index
Python iterate list with index
Python split string by comma
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module