Is this what you are looking for?

In [36]: data[np.arange(data.shape[0]),indices,:]
Out[36]: 
array([[7, 4],
       [7, 3],
       [4, 5],
       [8, 2],
       [5, 8]])
Answer from hpaulj on Stack Overflow
๐ŸŒ
Python Like You Mean It
pythonlikeyoumeanit.com โ€บ Module3_IntroducingNumpy โ€บ AccessingDataAlongMultipleDimensions.html
Accessing Data Along Multiple Dimensions in an Array โ€” Python Like You Mean It
Suppose we want the scores of all the students for Exam 2. We can slice from 0 through 3 along axis-0 (refer to the indexing diagram in the previous section) to include all the students, and specify index 1 on axis-1 to select Exam 2: >>> grades[0:3, 1] # Exam 2 scores for all students array([ 95, 100, 87]) As with Python sequences, you can specify an โ€œemptyโ€ slice to include all possible entries along an axis, by default: grades[:, 1] is equivalent to grades[0:3, 1], in this instance.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 67944674 โ€บ python-3d-array-indexing
numpy - Python 3D Array Indexing - Stack Overflow
In numpy the leading/first dimension is outermost, slowest. Trailing/last is inner, and fastest varying. The default indexing of the array is the same order as indexing the nest list you used to create it.
๐ŸŒ
Regenerativetoday
regenerativetoday.com โ€บ indexing-and-slicing-of-1d-2d-and-3d-arrays-using-numpy
Indexing and Slicing of 1D, 2D and 3D Arrays Using Numpy โ€“ Regenerative
Row index should be represented as 0:2. Column index is 1:4 as the elements are in first, second and third column. Combining all together: ... I hope this helps. Please try with different numbers and slices to learn more.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.5.dev0 Manual
The simplest case of indexing with N integers returns an array scalar representing the corresponding item. 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.
๐ŸŒ
Pythoninformer
pythoninformer.com โ€บ python-libraries โ€บ numpy โ€บ index-and-slice
PythonInformer - Indexing and slicing numpy arrays
We can create a 3 dimensional numpy array from a python list of lists of lists, like this: import numpy as np a3 = np.array([[[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[20, 21, 22], [23, 24, 25], [26, 27, 28]], [[30, 31, 32], [33, 34, 35], [36, 37, 38]]]) ... This selects matrix index 2 (the final matrix), row 0, column 1, giving a value 31. You can access any row or column in a 3D array.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ select-elements-from-3d-numpy-array-using-integer-indexing.php
Select elements from 3D NumPy array using integer Indexing
Write a NumPy program that creates a 3D NumPy array and uses integer array indexing to select elements along specific axes. Sample Solution: Python Code: import numpy as np # Create a 3D NumPy array of shape (3, 4, 5) with random integers array_3d ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ indexing-multi-dimensional-arrays-in-python-using-numpy
Indexing Multi-dimensional arrays in Python using NumPy | GeeksforGeeks
April 28, 2025 - Every array element has a particular index associated with them. Indexing starts at 0 and goes on till the length of array-1. In the previous example, arr_b has 5 elements within itself.
Find elsewhere
๐ŸŒ
YouTube
youtube.com โ€บ watch
Indexing and slicing 3D array using numpy in python - YouTube
This video demonstrates slicing and indexing of a 3D array using bumpy library in python
Published ย  October 23, 2023
๐ŸŒ
YouTube
youtube.com โ€บ watch
Indexing and Slicing of 1D, 2D and 3D Arrays Using Numpy - YouTube
This video starts with basic indexing and shows some more advanced techniques of indexing and slicing. I made a blog post explaining array indexing and slici...
Published ย  April 10, 2020
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-numpy-3d-array
3D Arrays In Python Using NumPy
May 16, 2025 - Slicing and indexing in 3D arrays allow precise access to subarrays and elements, making data manipulation intuitive and efficient. ... Iterating through 3D Python arrays in NumPy can be done using traditional nested loops or with efficient built-in tools like np.nditer.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ numpy-index-3d-array-with-index-of-last-axis-stored-in-2d-array
Numpy: Index 3D array with index of last axis stored in 2D array - GeeksforGeeks
July 23, 2025 - This technique leverages the numpy.take_along_axis function to efficiently select elements from a multidimensional array based on complex indexing conditions. By following the step-by-step guide, you can apply this method to your own data manipulation and analysis tasks, making your code more efficient and easier to understand. NumPy's advanced indexing capabilities are a testament to its power and flexibility, making it an indispensable tool for anyone working with numerical data in Python.
Top answer
1 of 4
16

You can use choose to make the selection:

>>> z_indices.choose(val_arr)
array([[ 9,  1, 20],
       [ 3,  4, 14],
       [24,  7, 17]])

The function choose is incredibly useful, but can be somewhat tricky to make sense of. Essentially, given an array (val_arr) we can make a series of choices (z_indices) from each n-dimensional slice along the first axis.

Also: any fancy indexing operation will create a new array rather than a view of the original data. It is not possible to index val_arr with z_indices without creating a brand new array.

2 of 4
7

With readability, np.choose definitely looks great.

If performance is of essence, you can calculate the linear indices and then use np.take or use a flattened version with .ravel() and extract those specific elements from val_arr. The implementation would look something like this -

def linidx_take(val_arr,z_indices):

    # Get number of columns and rows in values array
     _,nC,nR = val_arr.shape

     # Get linear indices and thus extract elements with np.take
    idx = nC*nR*z_indices + nR*np.arange(nR)[:,None] + np.arange(nC)
    return np.take(val_arr,idx) # Or val_arr.ravel()[idx]

Runtime tests and verify results -

Ogrid based solution from here is made into a generic version for these tests, like so :

In [182]: def ogrid_based(val_arr,z_indices):
     ...:   v_shp = val_arr.shape
     ...:   y,x = np.ogrid[0:v_shp[1], 0:v_shp[2]]
     ...:   return val_arr[z_indices, y, x]
     ...: 

Case #1: Smaller datasize

In [183]: val_arr = np.random.rand(30,30,30)
     ...: z_indices = np.random.randint(0,30,(30,30))
     ...: 

In [184]: np.allclose(z_indices.choose(val_arr),ogrid_based(val_arr,z_indices))
Out[184]: True

In [185]: np.allclose(z_indices.choose(val_arr),linidx_take(val_arr,z_indices))
Out[185]: True

In [187]: %timeit z_indices.choose(val_arr)
1000 loops, best of 3: 230 ยตs per loop

In [188]: %timeit ogrid_based(val_arr,z_indices)
10000 loops, best of 3: 54.1 ยตs per loop

In [189]: %timeit linidx_take(val_arr,z_indices)
10000 loops, best of 3: 30.3 ยตs per loop

Case #2: Bigger datasize

In [191]: val_arr = np.random.rand(300,300,300)
     ...: z_indices = np.random.randint(0,300,(300,300))
     ...: 

In [192]: z_indices.choose(val_arr) # Seems like there is some limitation here with bigger arrays.
Traceback (most recent call last):

  File "<ipython-input-192-10c3bb600361>", line 1, in <module>
    z_indices.choose(val_arr)

ValueError: Need between 2 and (32) array objects (inclusive).


In [194]: np.allclose(linidx_take(val_arr,z_indices),ogrid_based(val_arr,z_indices))
Out[194]: True

In [195]: %timeit ogrid_based(val_arr,z_indices)
100 loops, best of 3: 3.67 ms per loop

In [196]: %timeit linidx_take(val_arr,z_indices)
100 loops, best of 3: 2.04 ms per loop
๐ŸŒ
Physics Forums
physicsforums.com โ€บ other sciences โ€บ programming and computer science
3D Numpy Array indices....
February 26, 2021 - ... Hello, I am clear on 1D and 2D Numpy arrays, how to create them and address them). ... 3D array: list containing lists which contain lists as elements Array elements can be address using indices as a[], a[][], a[][][] respectively.
Top answer
1 of 3
13

You actually have a special case where it would be simpler and more efficient to do the following:

Create the data:

>>> arr
array([[[ 6,  9,  4],
        [ 5,  2,  1],
        [10, 15, 30]],

       [[ 9,  0,  1],
        [ 4,  6,  4],
        [ 8,  3,  9]],

       [[ 6,  7,  4],
        [ 0,  1,  6],
        [ 4,  0,  1]]])

The expected value:

>>> index_pos = np.where((arr[:,:,0]==10) & (arr[:,:,1]==15) & (arr[:,:,2]==30))
>>> index_pos
(array([0]), array([2]))

Use broadcasting to do this simultaneously:

>>> arr == np.array([10,15,30])
array([[[False, False, False],
        [False, False, False],
        [ True,  True,  True]],

       [[False, False, False],
        [False, False, False],
        [False, False, False]],

       [[False, False, False],
        [False, False, False],
        [False, False, False]]], dtype=bool)

>>> np.where( np.all(arr == np.array([10,15,30]), axis=-1) )
(array([0]), array([2]))

If the indices you want are not contiguous you can do something like this:

ind_vals = np.array([0,2])
where_mask = (arr[:,:,ind_vals] == values)

Broadcast when you can.

Spurred by @Jamie's comment, some interesting things to consider:

arr = np.random.randint(0,100,(5000,5000,3))

%timeit np.all(arr == np.array([10,15,30]), axis=-1)
1 loops, best of 3: 614 ms per loop

%timeit ((arr[:,:,0]==10) & (arr[:,:,1]==15) & (arr[:,:,2]==30))
1 loops, best of 3: 217 ms per loop

%timeit tmp = (arr == np.array([10,15,30])); (tmp[:,:,0] & tmp[:,:,1] & tmp[:,:,2])
1 loops, best of 3: 368 ms per loop

The question becomes, why is this?:

First off examine:

%timeit (arr[:,:,0]==10)
10 loops, best of 3: 51.2 ms per loop

%timeit (arr == np.array([10,15,30]))
1 loops, best of 3: 300 ms per loop

One would expect that arr == np.array([10,15,30]) would be at worse case 1/3 the speed of arr[:,:,0]==10. Anyone have an idea why this is not the case?

Then when combining the final axis there are many ways to accomplish this.

tmp = (arr == np.array([10,15,30]))

method1 = np.all(tmp,axis=-1)
method2 = (tmp[:,:,0] & tmp[:,:,1] & tmp[:,:,2])
method3 = np.einsum('ij,ij,ij->ij',tmp[:,:,0] , tmp[:,:,1] , tmp[:,:,2])

np.allclose(method1,method2)
True
np.allclose(method1,method3)
True

%timeit np.all(tmp,axis=-1)
1 loops, best of 3: 318 ms per loop

%timeit (tmp[:,:,0] & tmp[:,:,1] & tmp[:,:,2])
10 loops, best of 3: 68.2 ms per loop

%timeit np.einsum('ij,ij,ij->ij',tmp[:,:,0] , tmp[:,:,1] , tmp[:,:,2])
10 loops, best of 3: 38 ms per loop

The einsum speed up is well defined elsewhere, but it seems odd to me that there is such a difference between all and consecutive &'s.

2 of 3
6

The and operator won't work in this case.

index_pos = numpy.where(array[:,:,0]==10 and array[:,:,1]==15 and array[:,:,2]==30)

Give this a try:

index_pos = numpy.where((array[:,:,0]==10) & (array[:,:,1]==15) & (array[:,:,2]==30))
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ boolean-indexing-on-3d-numpy-arrays-with-conditions.php
Boolean Indexing on 3D NumPy arrays with conditions
Write a NumPy program that creates a 3D NumPy array and use boolean indexing to select elements along one axis based on conditions applied to another axis. Sample Solution: Python Code: import numpy as np # Create a 3D NumPy array of shape (3, ...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ array-indexing
Numpy Array Indexing (With Examples)
To access an element of a 3D array, we use three indices separated by commas. ... The third index refers to the column.
๐ŸŒ
Pluralsight
pluralsight.com โ€บ tech insights & how-to guides โ€บ tech guides & tutorials
Working with Numpy Arrays: Indexing & Slicing | Pluralsight
For example, let me define a one-dimensional array ... Index โ€˜3โ€™ represents the starting element of the slice and it's inclusive. Index โ€˜6โ€™ represents the stopping element of the slice and itโ€™s exclusive.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ select-elements-from-3d-numpy-array-using-fancy-indexing.php
Select elements from 3D NumPy array using Fancy indexing
Write a NumPy program that creates a 3D NumPy array and uses fancy indexing to select elements from specific rows and columns. Sample Solution: Python Code: import numpy as np # Create a 3D NumPy array of shape (3, 4, 5) array_3d = np.random.randint(0, 100, size=(3, 4, 5)) # Define the row and column indices to select specific elements row_indices = np.array([0, 1, 2]) col_indices = np.array([1, 2, 3]) # Use fancy indexing to select elements from specific rows and columns selected_elements = array_3d[row_indices[:, np.newaxis], col_indices] # Print the original 3D array and the selected elemen