How to find the Index of value in Numpy Array ?
Last Updated :
09 Aug, 2024
In this article, we are going to find the index of the elements present in a Numpy array.
Using where() Method
where() method is used to specify the index of a particular element specified in the condition.
Syntax: numpy.where(condition[, x, y])
Example 1: Get index positions of a given value
Here, we find all the indexes of 3 and the index of the first occurrence of 3, we get an array as output and it shows all the indexes where 3 is present.
Python
# import numpy package
import numpy as np
# create an numpy array
a = np.array([1, 2, 3, 4, 8, 6, 7, 3, 9, 10])
# display index value of 3
print("All index value of 3 is: ", np.where(a == 3)[0])
print("First index value of 3 is: ",np.where(a==3)[0][0])
Output:
All index value of 3 is: [2 7]
First index value of 3 is: 2
Example 2: Print First Index Position of Several Values
Here, we are printing the index number of all values present in the values array.
Python
# import numpy package
import numpy as np
# create an numpy array
a = np.array([1, 2, 3, 4, 8, 6, 2, 3, 9, 10])
values = np.array([2, 3, 10])
# index of first occurrence of each value
sorter = np.argsort(a)
print("index of first occurrence of each value: ",
sorter[np.searchsorted(a, values, sorter=sorter)])
Output:
index of first occurrence of each value: [1 2 9]
Example 3: Get the index of elements based on multiple conditions
Get the index of elements with a value less than 20 and greater than 12
Python
# Create a numpy array
a = 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 12
print("Index of elements with value less\
than 20 and greater than 12 are: \n",
np.where((a > 12) & (a < 20)))
Output:
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 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.
Python
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
Using ndenumerate() function to find the Index of value
It is usually used to find the first occurrence of the element in the given numpy array.
Python
import numpy as np
def ind(array, item):
for idx, val in np.ndenumerate(array):
if val == item:
return idx
# If no item was found return None, other return types might be a problem due to
# numbas type inference.
a = np.array([11, 12, 13, 14, 15, 16, 17, 15,
11, 12, 14, 15, 16, 17, 18, 19, 20])
print(ind(a,11))
Output:
(6,)
Using enumerate() function to find the Index of value
Here we are using the enumerate function and then checking the value with the target value.
Python
import numpy as np
a = np.array([11, 12, 13, 14, 15, 16, 17, 15,
11, 12, 14, 15, 16, 17, 18, 19, 20])
print(next(i for i, x in
enumerate(a) if x == 17))
Output:
6
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Machine Learning Tutorial Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.It can
5 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Linear Regression in Machine learning Linear regression is a type of supervised machine-learning algorithm that learns from the labelled datasets and maps the data points with most optimized linear functions which can be used for prediction on new datasets. It assumes that there is a linear relationship between the input and output, mea
15+ min read
Support Vector Machine (SVM) Algorithm Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or
9 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read