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?

Discussions

How to index a numpy array with another numpy array in python - Stack Overflow
I am trying to index an np.array with another array so that I can have zeros everywhere after a certain index but it gives me the error TypeError: only integer scalar arrays can be converted to a ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Get array with another array indexing with NumPy - Stack Overflow
#how to i get this array Numpy with "arr_idx_num_3" More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Index a numpy array with another array - Stack Overflow
I feel silly, because this is such a simple thing, but I haven't found the answer either here or anywhere else. Is there no straightforward way of indexing a numpy array with another? Say I have ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 4, 2015
python - Indexing multidimensional Numpy array with another array - Stack Overflow
Consider the following numpy array. ... But when I index it with another numpy array but with similar elements, it produces a different array, which is transpose of the above result. More on stackoverflow.com
๐ŸŒ stackoverflow.com
March 15, 2022
๐ŸŒ
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.5 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.
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_indexing.asp
NumPy Array Indexing
To access elements from 3-D arrays we can use comma separated integers representing the dimensions and the index of the element. Access the third element of the second array of the first array: import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) print(arr[0, 1, 2]) Try it Yourself ยป ... The first number represents the first dimension, which contains two arrays: [[1, 2, 3], [4, 5, 6]] and: [[7, 8, 9], [10, 11, 12]] Since we selected 0, we are left with the first array: [[1, 2, 3], [4, 5, 6]]
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".

Find elsewhere
๐ŸŒ
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
December 28, 2023 - 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.
๐ŸŒ
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")
๐ŸŒ
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
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ how-to-index-a-numpy-array-with-another-numpy-array.aspx
Python - How to index a NumPy array with another NumPy array?
Suppose that we are given two numpy arrays (say arr1 and arr2) and we need to index arr2 with arr1. By indexing arr2 with another array, the arr2 must return the corresponding values. Also, arr1 must contain all the values less than or equal to the length of values of arr2.
๐ŸŒ
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 โ€บ indexing-in-numpy
Basic Slicing and Advanced Indexing in NumPy Python - GeeksforGeeks
November 2, 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.
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ python
Indexing array thorough another array - Python - The freeCodeCamp Forum
January 12, 2021 - happy new year my name is frank, ... 1, 67] index1 = [1,2,3,4,5,6,7,8,9,10] index2 = [1,2,3,4,5,6,7,8,9,10] #arr = np.array([1,2,3,4,5,6,7,8,9,10]) #arrlist1 = np.array([23, 45, 21, 45, 2, 5, 11, 50, 1, 67]) a=0 b=0 sorted(range(len(listOfNum1)), key=lambda k: listOfNum1[k]) print("Elements in the list are : ") ...
๐ŸŒ
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