How to find the Index of value in Numpy Array ?

08 Jun, 2022
In this article, we are going to find the index of the elements present in a NumPy array. The where() method is used to specify the index of a particular element specified in the condition.
Syntax:
numpy.where(condition)
Here, the condition is the condition specified.
Example 1:
In this program, we are going to create an array with NumPy and display it. 
We can create an array with numpy by using the following syntax:
numpy.array([value1,value2,value3,.....,value n])
Python3
# import numpy packageimport numpy as np # create an numpy array with 1# to 10 elementsa = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) print(a)
Output:
[ 1  2  3  4  5  6  7  8  9 10]
Finding the index of 3
Python3
# display index value# of 3print(np.where(a == 3))
Output:
(array([2], dtype=int64),)
Finding an index value of 9
Python3
# display index value of 9print(np.where(a == 9))
Output:
(array([8], dtype=int64),)
We can also get the index of elements based on multiple conditions. Those conditions are specified in the where() function.
Example 2:
Get the index of elements with a value less than 20 and greater than 12
Python3
# Create a numpy array from a list of numbers# from 11 to 20a = np.array([11, 12, 13, 14, 15, 16, 17, 15,                11, 12, 14, 15, 16, 17, 18, 19, 20]) # Get the index of elements with value less# than 20 and greater than 12print("The numbers index locations with the index of \elements with value less than 20 and greater than 12 are ",      np.where((a > 12) & (a < 20)))
Output:
The numbers index locations with the index of elements with value less than 20 and greater than 12 are  (array([ 2,  3,  4,  5,  6,  7, 10, 11, 12, 13, 14, 15], dtype=int64),)
Get the index of elements with a value less than 20 and greater than 12
Python3
# Create a numpy array from a list of# numbers from 11 to 20a = np.array([11, 12, 13, 14, 15, 16, 17, 15,              11, 12, 14, 15, 16, 17, 18, 19, 20]) # Get the index of elements with value less# than 20 or greater than 12print("The numbers index locations with the index of \elements with value less than 20 or  greater than 12 are ",      np.where((a > 12) | (a < 20)))
Output:
The numbers index locations with the index of elements with value less than 20 or  greater than 12 are  (array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16]),)
Example 3:
Display element index where values less than 20
Python3
# create numpy array elementsa = np.array([2, 3, 4, 5, 6, 45, 67, 34]) # display element index where values# less than 20print("element index where values less than 20 : ",      np.where(a < 20))
Output:
element index where values less than 20 :  (array([0, 1, 2, 3, 4], dtype=int64),)
Example 4:
Display element index where values greater than 20
Python3
# create numpy array elementsa = np.array([2, 3, 4, 5, 6, 45, 67, 34]) # display element index where values# greater than 20print("element index where values greater than 20 : ",      np.where(a > 20))
Output:
element index where values greater than 20 :  (array([5, 6, 7], dtype=int64),)
Create a NumPy array.iterate over the array and compare the element in the array with the given array.If the element matches, print the index.
Create a NumPy array.
iterate over the array and 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 elementsa = np.array([2, 3, 4, 5, 6, 45, 67, 34]) # display element index where value = 45 index_of_element = -1for 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")
element index  : 5
pulamolusaimohan
Picked
Python numpy-Indexing
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.