Use np.where to get the indices where a given condition is True.
Examples:
For a 2D np.ndarray called a:
Use with AIi, j = np.where(a == value) # when comparing arrays of integers
i, j = np.where(np.isclose(a, value)) # when comparing floating-point arrays
For a 1D array:
Use with AIi, = np.where(a == value) # integers
i, = np.where(np.isclose(a, value)) # floating-point
Note that this also works for conditions like >=, <=, != and so forth...
You can also create a subclass of np.ndarray with an index() method:
Use with AIclass myarray(np.ndarray):
def __new__(cls, *args, **kwargs):
return np.array(*args, **kwargs).view(myarray)
def index(self, value):
return np.where(self == value)
Testing:
Use with AIa = myarray([1,2,3,4,4,4,5,6,4,4,4])
a.index(4)
#(array([ 3, 4, 5, 8, 9, 10]),)
Answer from Saullo G. P. Castro on Stack OverflowNumPy
numpy.org โบ doc โบ stable โบ user โบ basics.indexing.html
Indexing on ndarrays โ NumPy v2.5 Manual
As in Python, all indices are zero-based: for the i-th index \(n_i\), the valid range is \(0 \le n_i < d_i\) where \(d_i\) is the i-th element of the shape of the array. Negative indices are interpreted as counting from the end of the array (i.e., if \(n_i < 0\), it means \(n_i + d_i\)). All arrays generated by basic slicing are always views of the original array. ... NumPy slicing creates a view instead of a copy as in the case of built-in Python sequences such as string, tuple and list.
09:33
Numpy Array Indexing - YouTube
13:09
NumPy Tutorial #2: Indexing and Slicing - YouTube
03:36
NumPy: What Is Array Indexing, and Why Is It Useful? - YouTube
09:22
NumPy Array Indexing Explained | Access Elements, Rows & Columns ...
09:46
NumPy Array Indexing | Array Indexing in NumPy - YouTube
03:00
NumPy 1D Array Indexing Basics | Access Elements in Arrays Tutorial ...
Top answer 1 of 6
200
Use np.where to get the indices where a given condition is True.
Examples:
For a 2D np.ndarray called a:
Use with AIi, j = np.where(a == value) # when comparing arrays of integers
i, j = np.where(np.isclose(a, value)) # when comparing floating-point arrays
For a 1D array:
Use with AIi, = np.where(a == value) # integers
i, = np.where(np.isclose(a, value)) # floating-point
Note that this also works for conditions like >=, <=, != and so forth...
You can also create a subclass of np.ndarray with an index() method:
Use with AIclass myarray(np.ndarray):
def __new__(cls, *args, **kwargs):
return np.array(*args, **kwargs).view(myarray)
def index(self, value):
return np.where(self == value)
Testing:
Use with AIa = myarray([1,2,3,4,4,4,5,6,4,4,4])
a.index(4)
#(array([ 3, 4, 5, 8, 9, 10]),)
2 of 6
34
You can convert a numpy array to list and get its index .
for example:
Use with AItmp = [1,2,3,4,5] #python list
a = numpy.array(tmp) #numpy array
i = list(a).index(2) # i will return index of 2, which is 1
this is just what you wanted.
Programiz
programiz.com โบ python-programming โบ numpy โบ array-indexing
Numpy Array Indexing (With Examples)
June 1, 2023 - Note: Since the last element of ... index 5 is out of bounds for axis 0 with size 5 ยท We can use indices to change the value of an element in a NumPy array....
NumPy
numpy.org โบ devdocs โบ user โบ basics.indexing.html
Indexing on ndarrays โ NumPy v2.6.dev0 Manual
As in Python, all indices are zero-based: for the i-th index \(n_i\), the valid range is \(0 \le n_i < d_i\) where \(d_i\) is the i-th element of the shape of the array. Negative indices are interpreted as counting from the end of the array (i.e., if \(n_i < 0\), it means \(n_i + d_i\)). All arrays generated by basic slicing are always views of the original array. ... NumPy slicing creates a view instead of a copy as in the case of built-in Python sequences such as string, tuple and list.
Python Like You Mean It
pythonlikeyoumeanit.com โบ Module3_IntroducingNumpy โบ BasicIndexing.html
Introducing Basic and Advanced Indexing โ Python Like You Mean It
Thus far we have seen that we can access the contents of a NumPy array by specifying an integer or slice-object as an index for each one of its dimensions. Indexing into and slicing along the dimensions of an array are known as basic indexing.
DataCamp
datacamp.com โบ doc โบ numpy โบ array-indexing
NumPy Array Indexing
NumPy array indexing is used to extract or modify elements in an array based on their indices.
NumPy
numpy.org โบ devdocs โบ user โบ how-to-index.html
How to index ndarrays โ NumPy v2.6.dev0 Manual
To index specific elements in each column, make use of Advanced indexing as below: >>> arr = np.arange(3*4).reshape(3, 4) >>> arr array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> column_indices = [[1, 3], [0, 2], [2, 2]] >>> np.arange(arr.shape[0]) array([0, 1, 2]) >>> row_indices = np.arange(arr.shape[0])[:, np.newaxis] >>> row_indices array([[0], [1], [2]])
NumPy
numpy.org โบ doc โบ 1.21 โบ reference โบ arrays.indexing.html
Indexing โ NumPy v1.21 Manual
June 22, 2021 - As in Python, all indices are zero-based: for the i-th index \(n_i\), the valid range is \(0 \le n_i < d_i\) where \(d_i\) is the i-th element of the shape of the array. Negative indices are interpreted as counting from the end of the array (i.e., if \(n_i < 0\), it means \(n_i + d_i\)). All arrays generated by basic slicing are always views of the original array. ... NumPy slicing creates a view instead of a copy as in the case of builtin Python sequences such as string, tuple and list.
SciPy
docs.scipy.org โบ doc โบ โบ numpy-1.11.0 โบ user โบ basics.indexing.html
Indexing โ NumPy v1.11 Manual
May 29, 2016 - Single element indexing for a 1-D ... indices for indexing from the end of the array. ... Unlike lists and tuples, numpy arrays support multidimensional indexing for multidimensional arrays....
NumPy
numpy.org โบ doc โบ 1.16 โบ reference โบ arrays.indexing.html
Indexing โ NumPy v1.16 Manual
February 18, 2020 - For example, x[1:10:5,::-1] can also be implemented as obj = (slice(1,10,5), slice(None,None,-1)); x[obj] . This can be useful for constructing generic code that works on arrays of arbitrary dimension. ... The newaxis object can be used in all slicing operations to create an axis of length one. newaxis is an alias for โNoneโ, and โNoneโ can be used in place of this with the same result. Advanced indexing is triggered when the selection object, obj, is a non-tuple sequence object, an ndarray (of data type integer or bool), or a tuple with at least one sequence object or ndarray (of data type integer or bool).
NumPy
numpy.org โบ doc โบ 1.20 โบ reference โบ arrays.indexing.html
Indexing โ NumPy v1.20 Manual
January 31, 2021 - As in Python, all indices are zero-based: for the i-th index , the valid range is where is the i-th element of the shape of the array. Negative indices are interpreted as counting from the end of the array (i.e., if , it means ). All arrays generated by basic slicing are always views of the original array. ... NumPy slicing creates a view instead of a copy as in the case of builtin Python sequences such as string, tuple and list.
Problem Solving with Python
problemsolvingwithpython.com โบ 05-NumPy-and-Arrays โบ 05.05-Array-Indexing
Array Indexing - Problem Solving with Python
Elements in NumPy arrays can be accessed by indexing. Indexing is an operation that pulls out a select set of values from an array. The index of a value in an array is that value's location within the array.
NumPy
numpy.org โบ doc โบ 1.26 โบ reference โบ arrays.indexing.html
Indexing routines โ NumPy v1.26 Manual
Indexing on ndarrays ยท Translates slice objects to concatenation along the second axis
NumPy
numpy.org โบ doc โบ 2.2 โบ user โบ basics.indexing.html
Indexing on ndarrays โ NumPy v2.2 Manual
As in Python, all indices are zero-based: for the i-th index \(n_i\), the valid range is \(0 \le n_i < d_i\) where \(d_i\) is the i-th element of the shape of the array. Negative indices are interpreted as counting from the end of the array (i.e., if \(n_i < 0\), it means \(n_i + d_i\)). All arrays generated by basic slicing are always views of the original array. ... NumPy slicing creates a view instead of a copy as in the case of built-in Python sequences such as string, tuple and list.
GeeksforGeeks
geeksforgeeks.org โบ python โบ numpy-slicing-and-indexing
Basic Slicing and Advanced Indexing in NumPy - GeeksforGeeks
Indexing with index arrays lets you fetch multiple elements from a NumPy array at once using their index positions.
Published: November 4, 2025
Readthedocs
scipy-cookbook.readthedocs.io โบ items โบ Indexing.html
Indexing numpy arrays โ SciPy Cookbook documentation
June 1, 2008 - In its simplest (and most common) incarnation, you simply supply a single boolean array as index, the same shape as the original array: ... You then get back a one-dimensional array of the elements for which the condition is True. (Note that the array must be one-dimensional, since the boolean ...