According the NumPy tutorial, the correct way to do it is:

a[tuple(b)]
Answer from JoshAdel on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ indexing a np.array with another np.array
r/learnpython on Reddit: Indexing a np.array with another np.array
July 14, 2022 -

Indexing one array with another array has different behavior than if I index with the same array without explicitly casting it to a numpy array first (i.e. I leave it as a list of lists). I can't find the pages in the documentation that explain this kind of indexing

Example:

  #make a 5x5 matrix for testing, the numbers arent important 
  a = np.random.rand(5,5)

  #another arbitrary 5x5 matrix
  b = [[0, 0, 0, 0, 1],
         [0, 0, 0, 1, 1],
         [0, 0, 1, 1, 0],
         [0, 1, 1, 0, 0],
         [1, 1, 0, 0, 0]]

  c = np.array(b)

  a[b] #gives the error "too many indices for array: array is 2-dimensional, but 5 were indexed"

  a[tuple(c)] #gives the same error as a[b]

  a[c] #for some reason this works, and it returns a 5x5x5 matrix 

So the behavior changes when I convert the list of lists to a numpy array. And I can't really tell what it's doing by looking at the output of a[c]. It seems to be switching the rows around somehow but I'm confused at why it returns five copies of the original matrix. Is there any page in the documentation that describes this type of indexing?

๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_indexing.asp
NumPy Array Indexing
The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
๐ŸŒ
Kanoki
kanoki.org โ€บ 2020 โ€บ 07 โ€บ 05 โ€บ numpy-index-array-with-another-array
Index a Numpy Array by another Array | kanoki
July 5, 2020 - This returns Array B with index ranging from 0 to 24. Now we will use the take() method to get the Array A arranged by the index of new Array B ยท m,n = A.shape np.take(A,B + n*np.arange(m)[:,None]) ... array([[1. , 0.32, 0.63, 0.88, 0.35], [0.22, 0.98, 0.96, 0.69, 0.23], [0.19, 0.58, 0.7 , 0.09, 0.51], [0.46, 0.62, 0.98, 0.94, 0.42], [0.48, 0.23, 0.59, 0.17, 0.98]]) Numpy take_along_axis() method iterates over matching 1d slices oriented along the specified axis in the index and data arrays, and uses the former to look up values in the latter
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.4 Manual
An integer, i, returns the same values as i:i+1 except the dimensionality of the returned object is reduced by 1. In particular, a selection tuple with the p-th element an integer (and all other entries :) returns the corresponding sub-array with dimension N - 1. If N = 1 then the returned object is an array scalar. These objects are explained in Scalars. If the selection tuple has all entries : except the p-th entry which is a slice object i:j:k, then the returned array has dimension N formed by stacking, along the p-th axis, the sub-arrays returned by integer indexing of elements i, i+k, โ€ฆ, i + (m - 1) k < j.
๐ŸŒ
Python Data Science Handbook
jakevdp.github.io โ€บ PythonDataScienceHandbook โ€บ 02.07-fancy-indexing.html
Fancy Indexing | Python Data Science Handbook
Notice that the first value in the result is X[0, 2], the second is X[1, 1], and the third is X[2, 3]. The pairing of indices in fancy indexing follows all the broadcasting rules that were mentioned in Computation on Arrays: Broadcasting. So, for example, if we combine a column vector and a row vector within the indices, we get a two-dimensional result:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ numpy-indexing
Numpy Array Indexing - GeeksforGeeks
December 17, 2025 - ... The combined condition selects elements greater than 10 and less than 30 resulting in [15, 20, 25]. It is also known as Advanced Indexing which allows us access elements of an array by using another array or list of indices.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ numpy-for-every-element-in-one-array-find-the-index-in-another-array.aspx
Python - NumPy: For every element in one array, find the index in another array
# Import numpy import numpy as np # Creating two numpy arrays arr1 = np.array([1, 2, 3, 4]) arr2 = np.array([1, 2, 5, 7]) # Display original arrays print("Original array 1:\n",arr1,"\n") print("Original array 2:\n",arr2,"\n") # Checking the indices of elements # based on another array res = np.where(np.in1d(arr1, arr2))[0] # Display the result print("Indices of elements:\n",res,"\n")
Find elsewhere
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.5.dev0 Manual
An integer, i, returns the same values as i:i+1 except the dimensionality of the returned object is reduced by 1. In particular, a selection tuple with the p-th element an integer (and all other entries :) returns the corresponding sub-array with dimension N - 1. If N = 1 then the returned object is an array scalar. These objects are explained in Scalars. If the selection tuple has all entries : except the p-th entry which is a slice object i:j:k, then the returned array has dimension N formed by stacking, along the p-th axis, the sub-arrays returned by integer indexing of elements i, i+k, โ€ฆ, i + (m - 1) k < j.
๐ŸŒ
YouTube
youtube.com โ€บ udacity
Indexing an array with another array - YouTube
This video is part of the Udacity course "Machine Learning for Trading". Watch the full course at https://www.udacity.com/course/ud501
Published ย  June 6, 2016
Views ย  3K
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ array-indexing
Numpy Array Indexing (With Examples)
In NumPy, each element in an array is associated with a number.In NumPy, each element in an array is associated with a number. The number is known as an array index. Let's see an example to demonstrate NumPy array indexing. Array Indexing in NumPy In the above array, 5 is the 3rd element.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ indexing-in-numpy
Basic Slicing and Advanced Indexing in NumPy Python - GeeksforGeeks
November 1, 2022 - In case of slice, a view or shallow copy of the array is returned but in index array a copy of the original array is returned. Numpy arrays can be indexed with other arrays or any other sequence with the exception of tuples.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ python-numpy-exercise-100.php
Python NumPy: Take values from a source array and put them at specified indices of another array - w3resource
Create a function that takes a source array and target indices, then uses np.put to insert new values and returns the modified array. Test the put operation on an array by ensuring that the specified indices have been updated correctly.
Top answer
1 of 2
1

It can be done with array indexing but it doesn't feel natural.

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
d = np.array([2, 1, 3])

col_ix = [ 0, 0, 1, 1, 1, 2 ]  # column ix for each item to change                                   
row_ix = [ 2, 3, 1, 2, 3, 3 ]  # row index for each item to change

a[ row_ix, col_ix ] = 0

a 
# array([[1, 2, 3],
#        [4, 0, 6],
#        [0, 0, 9],
#        [0, 0, 0]])

With a for loop

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])

for ix_col, ix_row in enumerate( d ):  # iterate across the columns
    a[ ix_row:, ix_col ] = 0

a 
# array([[1, 2, 3],
#        [4, 0, 6],
#        [0, 0, 9],
#        [0, 0, 0]])
2 of 2
0

A widely used approach for this kind of problem is to construct a boolean mask, comparing the index array with the appropriate arange:

In [619]: mask = np.arange(4)[:,None]>=d
In [620]: mask
Out[620]: 
array([[False, False, False],
       [False,  True, False],
       [ True,  True, False],
       [ True,  True,  True]])
In [621]: a[mask]
Out[621]: array([ 5,  7,  8, 10, 11, 12])
In [622]: a[mask] = 0
In [623]: a
Out[623]: 
array([[1, 2, 3],
       [4, 0, 6],
       [0, 0, 9],
       [0, 0, 0]])

That's not necessarily faster than a row (or in this case column) iteration. Since slicing is basic indexing, it may be faster, even if done several times.

In [624]: for i,v in enumerate(d):
     ...:     print(a[v:,i])
     ...: 
[0 0]
[0 0 0]
[0]

Generally if a result involves multiple arrays or lists with different lengths, there isn't a "neat" multidimensional solution. Either iterate over those lists, or step back and "think outside the box".

๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ find-indices-of-matches-of-one-array-in-another-array.aspx
Python - Find indices of matches of one array in another array
To find indices of matches of one array in another array, we use numpy.in1d() with numpy.nonzero(). The numpy.in1d() is used to test whether each element of a 1-D array is also present in a second array.
๐ŸŒ
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. Unlike slicing, it returns a new copy of the data. Example: Here, we create an array in decreasing order and use another array ...
Published ย  November 4, 2025
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 1.16 โ€บ reference โ€บ arrays.indexing.html
Indexing โ€” NumPy v1.16 Manual
An integer, i, returns the same values as i:i+1 except the dimensionality of the returned object is reduced by 1. In particular, a selection tuple with the p-th element an integer (and all other entries :) returns the corresponding sub-array with dimension N - 1. If N = 1 then the returned object is an array scalar. These objects are explained in Scalars. If the selection tuple has all entries : except the p-th entry which is a slice object i:j:k, then the returned array has dimension N formed by concatenating the sub-arrays returned by integer indexing of elements i, i+k, โ€ฆ, i + (m - 1) k < j,