All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Find Index Of Value In NumPy Array

Last Updated : Sep 14, 2023

Python Find Index Of Value In NumPy Array

In this article we will show you the solution of python find index of value in numpy array, the NumPy package in Python offers robust tools for manipulating matrices and arrays.

Finding the index of a particular value within a NumPy array is a frequent task. For this, the numpy.where() function comes in extremely handy.

You can use the numpy.where() function to discover the index of a value within a NumPy array by passing the array and the target value as parameters.

The value's occurrence indices are included in the tuple of arrays that this function returns.

The indices of all instances where the value occurs will be returned. We will now discuss the idea of finding a value's index in a numpy array in Python.

Step By Step Guide On Python Find Index Of Value In Numpy Array :-

import numpy as np
# Create a NumPy array
arr = np.array([10, 20, 30, 40, 50])
# Define the target value
target_value = 30
# Find the indices where the target value occurs in the array
indices = np.where(arr == target_value)[0]
# Check if any indices were found
if len(indices) > 0:
    print(f"The value {target_value} was found at index(es): {', '.join(map(str, indices))}")
else:
    print(f"The value {target_value} was not found in the array.")
  1. To make things easier, we first import the NumPy library and alias it as np.
  2. When using arrays and matrices, NumPy is a powerful toolkit for Python numerical operations.
  3. Next, we use the np.array() function to build a NumPy array called arr.
  4. The array in this instance contains the following values: 10, 20, 30, 40, and 50.
  5. This array can be changed to fit your particular use case.
  6. The target_value variable is then defined and set to the value for which we are looking for the indexes.
  7. In this instance, target_value is set to 30. Change this value as necessary to suit your needs.
  8. Using the np. where() function, we can determine the array indexes where the desired value can be found.
  9. When the condition is satisfied, the expression arr == target_value compares every element of the array arr with the target_value and produces a Boolean array with True values.
  10. We use np.where() to access the first member [0] of the returned tuple of arrays by passing this Boolean array as an input.
  11. The target value's location indices are contained in this array.
  12. Then, by using len(indices) to determine the size of the indices array, we can determine whether any indices were discovered.
  13. If the length is greater than 0, it signifies the array contained the target value.
  14. In this case, we display the indices by using join() to turn the indices into strings, separating them with commas, and printing a message to show that the value was discovered.
  15. We output a message indicating this if the length of the indices is zero, which indicates that the target value was not located in the array.

Conclusion :-

As a result, we have successfully learnt how to use Python to determine a value's index in a numpy array.

We also discovered that the code effectively found the array indexes at which the target value appeared by utilising the np.where() function.

I hope this article on python find index of value in numpy array helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪