In [1]: import numpy as np
In [2]: a = np.array([[2,0],[3,0],[3,1],[5,0],[5,1],[5,2]])
In [3]: b = np.zeros((6,3), dtype='int32')

In [4]: b[a[:,0], a[:,1]] = 10

In [5]: b
Out[5]: 
array([[ 0,  0,  0],
       [ 0,  0,  0],
       [10,  0,  0],
       [10, 10,  0],
       [ 0,  0,  0],
       [10, 10, 10]])

Why it works:

If you index b with two numpy arrays in an assignment,

b[x, y] = z

then think of NumPy as moving simultaneously over each element of x and each element of y and each element of z (let's call them xval, yval and zval), and assigning to b[xval, yval] the value zval. When z is a constant, "moving over z just returns the same value each time.

That's what we want, with x being the first column of a and y being the second column of a. Thus, choose x = a[:, 0], and y = a[:, 1].

b[a[:,0], a[:,1]] = 10

Why b[a] = 10 does not work

When you write b[a], think of NumPy as creating a new array by moving over each element of a, (let's call each one idx) and placing in the new array the value of b[idx] at the location of idx in a.

idx is a value in a. So it is an int32. b is of shape (6,3), so b[idx] is a row of b of shape (3,). For example, when idx is

In [37]: a[1,1]
Out[37]: 0

b[a[1,1]] is

In [38]: b[a[1,1]]
Out[38]: array([0, 0, 0])

So

In [33]: b[a].shape
Out[33]: (6, 2, 3)

So let's repeat: NumPy is creating a new array by moving over each element of a and placing in the new array the value of b[idx] at the location of idx in a. As idx moves over a, an array of shape (6,2) would be created. But since b[idx] is itself of shape (3,), at each location in the (6,2)-shaped array, a (3,)-shaped value is being placed. The result is an array of shape (6,2,3).

Now, when you make an assignment like

b[a] = 10

a temporary array of shape (6,2,3) with values b[a] is created, then the assignment is performed. Since 10 is a constant, this assignment places the value 10 at each location in the (6,2,3)-shaped array. Then the values from the temporary array are reassigned back to b. See reference to docs. Thus the values in the (6,2,3)-shaped array are copied back to the (6,3)-shaped b array. Values overwrite each other. But the main point is you do not obtain the assignments you desire.

Answer from unutbu on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › user › basics.indexing.html
Indexing on ndarrays — NumPy v2.5 Manual
The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step (\(k\neq0\)). This selects the m elements (in the corresponding dimension) with index values i, i + k, …, i + (m - 1) k where \(m = q + (r\neq0)\) and q and r are the quotient and remainder ...
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_indexing.asp
NumPy Array Indexing
Think of 2-D arrays like a table with rows and columns, where the dimension represents the row and the index represents the column. Access the element on the first row, second column: import numpy as np arr = np.array([[1,2,3,4,5], [6,7,8,9,10]]) ...
Discussions

Is it possible to access multidimensional numpy array with a single index without a reshape?
But is there a property or a trick to access the elements with one index without using a reshape? I mean, it's a 2D array. Accessing it via a 1D index is reshaping it. More on reddit.com
🌐 r/learnpython
4
1
June 30, 2023
Numpy indices-ifs for 2d array rows?
You do that exactly as you might expect. See: arr2d = np.array([[0,1,2],[3,4,5],[6,7,8]]) arr2d[np.sum(arr2d, axis=1) > 10] = 2 Here, np.sum(…, axis=1) sums across the column dimension — row sums — to give a 1D array of length (# of rows of arr2d). So np.sum(arr2d, axis=1) > 10 returns the 1D Boolean array [False, True, True] Finally, when you use that to index arr2D, Numpy implicitly copies entries across columns to match the second dimension of arr2D, giving the 2D mask [[ False, False, False] [ True, True, True] [ True, True, True]] which is exactly what you want. More on reddit.com
🌐 r/learnpython
4
2
August 16, 2021
using np.where on a 2D array
Do an all-reduction after comparing the two arrays: index_that_matches = np.where((reduced == vector).all(1))[0][0] More on reddit.com
🌐 r/learnpython
3
2
September 5, 2022
Python optimization
🌐 r/Python
25
14
April 1, 2026
Top answer
1 of 2
67
In [1]: import numpy as np
In [2]: a = np.array([[2,0],[3,0],[3,1],[5,0],[5,1],[5,2]])
In [3]: b = np.zeros((6,3), dtype='int32')

In [4]: b[a[:,0], a[:,1]] = 10

In [5]: b
Out[5]: 
array([[ 0,  0,  0],
       [ 0,  0,  0],
       [10,  0,  0],
       [10, 10,  0],
       [ 0,  0,  0],
       [10, 10, 10]])

Why it works:

If you index b with two numpy arrays in an assignment,

b[x, y] = z

then think of NumPy as moving simultaneously over each element of x and each element of y and each element of z (let's call them xval, yval and zval), and assigning to b[xval, yval] the value zval. When z is a constant, "moving over z just returns the same value each time.

That's what we want, with x being the first column of a and y being the second column of a. Thus, choose x = a[:, 0], and y = a[:, 1].

b[a[:,0], a[:,1]] = 10

Why b[a] = 10 does not work

When you write b[a], think of NumPy as creating a new array by moving over each element of a, (let's call each one idx) and placing in the new array the value of b[idx] at the location of idx in a.

idx is a value in a. So it is an int32. b is of shape (6,3), so b[idx] is a row of b of shape (3,). For example, when idx is

In [37]: a[1,1]
Out[37]: 0

b[a[1,1]] is

In [38]: b[a[1,1]]
Out[38]: array([0, 0, 0])

So

In [33]: b[a].shape
Out[33]: (6, 2, 3)

So let's repeat: NumPy is creating a new array by moving over each element of a and placing in the new array the value of b[idx] at the location of idx in a. As idx moves over a, an array of shape (6,2) would be created. But since b[idx] is itself of shape (3,), at each location in the (6,2)-shaped array, a (3,)-shaped value is being placed. The result is an array of shape (6,2,3).

Now, when you make an assignment like

b[a] = 10

a temporary array of shape (6,2,3) with values b[a] is created, then the assignment is performed. Since 10 is a constant, this assignment places the value 10 at each location in the (6,2,3)-shaped array. Then the values from the temporary array are reassigned back to b. See reference to docs. Thus the values in the (6,2,3)-shaped array are copied back to the (6,3)-shaped b array. Values overwrite each other. But the main point is you do not obtain the assignments you desire.

2 of 2
4

TL;DR: Use advanced indexing: b[*a.T] = 10

You can also transpose the index array a, convert the result into a tuple and index the array b and assign a value. Converting the index array into a tuple (or unpacking it inside a []) ensures that multidimensional indexing works as expected. This is assignment by advanced indexing.

a = np.array([[2, 0], [3, 0], [3, 1], [5, 0], [5, 1], [5, 2]])
b = np.zeros((6,3), dtype ='int32')

b[*a.T] = 10
# or
b[tuple(a.T)] = 10
# or 
b[(*a.T,)] = 10
# or 
b[(*a.T.tolist(),)] = 10

All of them produce the expected output of

array([[ 0,  0,  0],
       [ 0,  0,  0],
       [10,  0,  0],
       [10, 10,  0],
       [ 0,  0,  0],
       [10, 10, 10]])
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-indexing
Numpy Array Indexing - GeeksforGeeks
December 17, 2025 - We can access elements by specifying row, column and depth indices like matrix[depth, row, column]. ... import numpy as np cube = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]]]) print(cube[1, 2, 0])
🌐
NumPy
numpy.org › devdocs › user › basics.indexing.html
Indexing on ndarrays — NumPy v2.6.dev0 Manual
The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step (\(k\neq0\)). This selects the m elements (in the corresponding dimension) with index values i, i + k, …, i + (m - 1) k where \(m = q + (r\neq0)\) and q and r are the quotient and remainder ...
🌐
NumPy
numpy.org › devdocs › user › how-to-index.html
How to index ndarrays — NumPy v2.6.dev0 Manual
>>> x = np.arange(2*2*3).reshape(2, 2, 3) % 7 # 3D example array >>> x array([[[0, 1, 2], [3, 4, 5]], [[6, 0, 1], [2, 3, 4]]]) >>> x_2d = np.reshape(x, (x.shape[0], -1)) >>> indices_2d = np.argmax(x_2d, axis=1) >>> indices_2d array([5, 0]) >>> np.unravel_index(indices_2d, x.shape[1:]) (array([1, 0]), array([2, 0]))
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module3_IntroducingNumpy › AccessingDataAlongMultipleDimensions.html
Accessing Data Along Multiple Dimensions in an Array — Python Like You Mean It
NumPy specifies the row-axis (students) ... each axis (dimension), to uniquely specify an element in this 2D array; the first number specifies an index along axis-0, the second specifies an index along axis-1....
Find elsewhere
🌐
Towards Data Science
towardsdatascience.com › home › latest › introducing numpy, part 2: indexing arrays
Introducing NumPy, Part 2: Indexing Arrays | Towards Data Science
January 13, 2025 - For example, specifying an integer index of 1 outputs the 1D array that comprises the second row of the 2D array: ... Slicing a 2D array also works along 1D arrays. Here we slice over rows, taking the last two: ... This produced a 2D array of shape (2, 3), meaning 2 rows and 3 columns. To obtain a whole column in the 2D array, use the following syntax: ... The colon (:) tells NumPy to take all the rows; the 1 selects only column 1, leaving you with only a 1D array from the center column of arr2d.
🌐
Uni-heidelberg
ita.uni-heidelberg.de › ~dullemond › lectures › python_2019 › py4sci_wed › Note on IndexOrdering.html
Index ordering in Numpy
The indices of 2-D arrays are ordered in the same way as matrix indices are. Example: ... The first index (here set to 1) counts rows (i.e. it counts from top to bottom). The second index (here set to 0) counts columns (i.e. it counts from left to right). This is exactly the way it is done ...
🌐
Programiz
programiz.com › python-programming › numpy › array-indexing
Numpy Array Indexing (With Examples)
Now, we'll see how we can access individual items from the array using the index number. We can use indices to access individual elements of a NumPy array.
🌐
GeeksforGeeks
geeksforgeeks.org › python › indexing-multi-dimensional-arrays-in-python-using-numpy
Indexing Multi-dimensional arrays in Python using NumPy - GeeksforGeeks
November 4, 2025 - import numpy as np arr = np.arange(20, 30, 2) print(arr) print(arr[2]) # Access by index print(arr[1:4]) # Slice from index 1 to 3 ... We use reshape() with arange() to convert a 1D array into a 2D array.
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module3_IntroducingNumpy › AdvancedIndexing.html
Advanced Indexing — Python Like You Mean It
The index-arrays must have the same shape as one another, and this common shape determines the shape of the resulting array. This is a form of advanced indexing, and thus a copy of the parent array’s data is created. NumPy also permits the use of a boolean-valued array as an index, to perform advanced indexing on an array.
🌐
Medium
medium.com › @whyamit404 › basics-of-numpy-array-indexing-9052e6d6b5cf
Basics of NumPy Array Indexing. If you think you need to spend $2,000… | by whyamit404 | Medium
February 9, 2025 - Whether you want a specific range or skip every other slice, NumPy makes it easy. Let’s dive into the details! Slicing allows you to grab a portion of your array using the syntax: start:stop:step Here’s how it works: Start: The index where your slice begins (inclusive).
🌐
Mdjubayerhossain
mdjubayerhossain.com › numpy › notebooks › 04_ArraySlicingandSubsetting.html
Array Indexing and Slicing — Introduction to NumPy
# Create a 2D array of students ... as many colons as needed to produce a complete indexing tuple ... NumPy arrays can be indexed with slices, but also with boolean or integer arrays (masks)....
🌐
Pluralsight
pluralsight.com › blog › tech guides & tutorials
Working with Numpy Arrays: Indexing & Slicing | Pluralsight
Note that both the column and the row indices start with 0. So if I need to access the value ‘10,’ use the index ‘3’ for the row and index ‘1’ for the column. ... Let’s go one level higher. To access a three-dimensional array, include the index for the third dimension as well.
🌐
APXML
apxml.com › courses › essential-numpy-pandas › chapter-3-numpy-array-indexing-slicing › accessing-single-elements
Access NumPy Array Elements
Think of the first index selecting the row and the second index selecting the column within that row. Visual representation of 2D array indexing. The row index comes first, followed by the column index.
🌐
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 - In this article, we have demonstrated how to index a 3D NumPy array using indices stored in a 2D array. This technique leverages the numpy.take_along_axis function to efficiently select elements from a multidimensional array based on complex indexing conditions.
🌐
Educative
educative.io › answers › how-does-array-indexing-work-using-numpy-in-python
How does array indexing work using NumPy in Python?
from numpy import array · # Create a 1D array · one_dim_arr = array([1, 2, 3, 4, 5, 6, 7]) # Slicing operation on 1D array · sliced_one_dim_array = one_dim_arr[1:4] print ("Elements from the index 1 to index 4:\n", sliced_one_dim_array) # Create a 2D array ·
🌐
Reddit
reddit.com › r/learnpython › numpy indices-ifs for 2d array rows?
r/learnpython on Reddit: Numpy indices-ifs for 2d array rows?
August 16, 2021 -

hello,

there is a feature in numpy that lets you find array elements with certain properties, for example:

import numpy as np
arr = np.array([5,6,2,6,7,9,2,1,4,7,0,6])
print(arr[arr>5])
=> [6, 6, 7, 9, 7, 6]

# this works great for quick substitutions:
arr2 = np.array([9,9,9,9,9,9,9,9,9,9,9,9])
arr[arr<5] = arr2[arr<5]
print(arr)
=> [5, 6, 9, 6, 7, 9, 9, 9, 9, 7, 9, 6]

# it even keeps dimensions:
arr2d = np.array([[0,1,2],[3,4,5],[6,7,8]])
arr2d[arr2d<4] = 99
print(arr2d)
=>[[99 99 99]
   [99  4  5]
   [ 6  7  8]]

Now to my question:

The 'if thingies' loop through every element of an array regardless of it's dimensions, creates a 1d bool array, which is then fed into the square brackets and then the operation is only performed if the bool at the current index is true.

Is there any way that this 'if' does not look at each individual element, but for example at a whole row? Can you specify how "deep" the search should be?

# task (not a real task, just demonstration): replace all rows that have a sum > 10 with a row containing only twos
#      replace?    yes     yes       no        yes        no
arr = np.array([[2,3,7], [9,1,1], [3,4,1], [4, 4, 4], [0, 7, 1]])
# this search now should only penetrate the first layer of the 2d array and not the second one, so i dont get individual numbers in my comparison/selection, but whole "rows/subarrays"
arr[*black magic*] = (2, 2, 2)
print(arr)
=> [[2,2,2], [2,2,2], [3,4,1], [2, 2, 2], [0, 7, 1]]

Thank you for your help!

I know this is very confusing and probably even more confusing if the person reading this is not me, so please tell me if what i wrote is utter nonsense

i know that you can do it with for loops or list comprehension, but i want to know if this specific form works, because i think it's a very cool feature