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
🌐
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]]) ...
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]])
Discussions

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
Python optimization
🌐 r/Python
25
14
April 1, 2026
Indexing a np.array with another np.array
Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you. I think I have detected some formatting issues with your submission: Python code found in submission text that's not formatted as code. If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting. Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here . More on reddit.com
🌐 r/learnpython
4
1
July 14, 2022
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
🌐
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])
🌐
Pluralsight
pluralsight.com › blog › 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. That's the reason why we did not get the value ‘6’ in the output. If you do not specify the starting and the stopping index you will get all the values. ... That's because if the indices are missing, by default, Numpy inserts the starting and stopping indices that select the entire 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.
🌐
Uni-heidelberg
ita.uni-heidelberg.de › ~dullemond › lectures › python_2019 › py4sci_wed › Note on IndexOrdering.html
Index ordering in Numpy
As you can see by the outcome of ff.shape: The x-index (which has 40 elements) is right, the y-index (which has 20 elements) is left. So the index-order is ff[index_y,index_x], i.e. (y,x). This can be confusing, because in mathematics we are used to set the order to (x,y). But from the python ...
Find elsewhere
🌐
Programiz
programiz.com › python-programming › numpy › array-indexing
Numpy Array Indexing (With Examples)
Let's see an example to demonstrate NumPy array indexing. ... In the above array, 5 is the 3rd element. However, its index is 2. This is because the array indexing starts from 0, that is, the first element of the array has index 0, the second element has index 1, and so on.
🌐
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).
🌐
NumPy
numpy.org › devdocs › user › basics.indexing.html
Indexing on ndarrays — NumPy v2.6.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.
🌐
DataCamp
campus.datacamp.com › courses › intro-to-python-for-data-science › chapter-4-numpy
2D NumPy Arrays | Python
You can think of the 2D numpy array ... of subsetting. Suppose you want the first row, and then the third element in that row. To select the row, you need the index 0 in square brackets....
🌐
NumPy
numpy.org › doc › stable › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.5 Manual
As with built-in Python sequences, NumPy arrays are “0-indexed”: the first element of the array is accessed using index 0, not 1.
🌐
w3resource
w3resource.com › python-exercises › numpy › index-and-select-elements-in-2d-numpy-array-using-tuple-of-arrays.php
Index and select elements in 2D NumPy Array using Tuple of arrays
April 29, 2025 - Create a 2D NumPy array named array_2d ... columns from which to select elements. ... Used a tuple of arrays (row_indices, col_indices) to index and select specific elements from array_2d....
🌐
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....
🌐
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 - First, we had to choose the second 2D array, which has an index of 1 because Python starts counting at 0. Next, we selected the third row using 2. Finally, we selected the first column using 0. The key is to work your way through the shape tuple in order.
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_iterating.asp
NumPy Array Iterating
Iterate through every scalar element of the 2D array skipping 1 element: import numpy as np arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) for x in np.nditer(arr[:, ::2]): print(x) Try it Yourself » · Enumeration means mentioning sequence number of somethings one by one. Sometimes we require corresponding index of the element while iterating, the ndenumerate() method can be used for those usecases.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Get and set values in an array using various indexing | note.nkmk.me
February 7, 2024 - Using this for indexing with [] selects True values, producing a flattened 1D array. ... print(a_2d > 5) # [[False False False False] # [False False True True] # [ True True True True]] print(a_2d[a_2d > 5]) # [ 6 7 8 9 10 11] ... Specify multiple conditions using & (AND), | (OR), and ~ (NOT) with parentheses (). Using and, or, not, or omitting parentheses results in an error. How to fix "ValueError: The truth value ... is ambiguous" in NumPy, pandas
🌐
Earth Data Science
earthdatascience.org › home
Slice (or Select) Data From Numpy Arrays | Earth Data Science - Earth Lab
September 23, 2019 - Use indexing to slice (i.e. select) data from one-dimensional and two-dimensional numpy arrays. In a previous chapter that introduced Python lists, you learned that Python indexing begins with [0], and that you can use indexing to query the value of items within Python lists.
🌐
GeeksforGeeks
geeksforgeeks.org › python › indexing-multi-dimensional-arrays-in-python-using-numpy
Indexing Multi-dimensional arrays in Python using NumPy - GeeksforGeeks
November 4, 2025 - Python · import numpy as np arr ... arr[1:4] -> elements at indices 1, 2, 3 -> [22, 24, 26] We use reshape() with arange() to convert a 1D array into a 2D array....
🌐
Utexas
johnfoster.pge.utexas.edu › numerical-methods-book › ScientificPython_Numpy.html
NumPy: Numerical Python
September 8, 2020 - First, the Python list comprehension ... Here the + 1 is broadcast across the array, i.e. each element has 1 added to it. ... Here we see that adding 1 to a million numbers in NumPy is significantly faster than using a Python list comprehension (which itself is much faster than a for loop would be in pure Python).
🌐
TutorialsPoint
tutorialspoint.com › python_data_structure › python_2darray.htm
Python - 2-D Array
One index referring to the main or parent array and another index referring to the position of the data element in the inner array.If we mention only one index then the entire inner array is printed for that index position.