In your array:

  • The x and t, are the beginning of the slice;
  • The y and t, are the end of the slice;
  • The i and m, are the step of the slice.

For example, let's define an 8x8 array:

z=[[x*y+x+y for x in range(8)] for y in range(8)]
z=np.asarray(z)

Out[1]:
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 1,  3,  5,  7,  9, 11, 13, 15],
       [ 2,  5,  8, 11, 14, 17, 20, 23],
       [ 3,  7, 11, 15, 19, 23, 27, 31],
       [ 4,  9, 14, 19, 24, 29, 34, 39],
       [ 5, 11, 17, 23, 29, 35, 41, 47],
       [ 6, 13, 20, 27, 34, 41, 48, 55],
       [ 7, 15, 23, 31, 39, 47, 55, 63]])

z.shape
Out[2]: (8, 8)

From row 0 until row 3 (excluding it) every 2 rows, will index like:

z[0:3:2]

Out[3]: 
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 2,  5,  8, 11, 14, 17, 20, 23]])

For columns:

z[:,1:6:3]

Out[4]: 
array([[ 1,  4],
       [ 3,  9],
       [ 5, 14],
       [ 7, 19],
       [ 9, 24],
       [11, 29],
       [13, 34],
       [15, 39]])

Combining rows and columns:

z[0:3:2, 0:3:2]

Out[5]: 
array([[0, 2],
       [2, 8]])
Answer from Pedro on Stack Overflow
🌐
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.
🌐
Scaler
scaler.com β€Ί home β€Ί topics β€Ί 2d array in python
2D Array in Python | Python Two-Dimensional Array - Scaler Topics
May 25, 2026 - Unlike a one dimensional array, which uses a single index for individual elements, a 2D array uses two indices to locate a value. 2D arrays in python are zero-indexed, which means counting indices start from zero rather than one; thus, zero is the first index in an array in python.
Discussions

How to find the index of a value in 2d array in Python? - Stack Overflow
Basically, it gives me only one of the index in each row [(0, 0), (1, 2)]. ... yes, its . I actually have a large 2d array and I got that from extracting an image. More on stackoverflow.com
🌐 stackoverflow.com
Help finding the index of a given value within a 2D array
Numpy is a great and powerful tool, but this is not one of the things it's good at. It would be much better for you to store the player's actual position, rather than searching for the player in a map. But if you really really want to: the function you want is np.argwhere. More on reddit.com
🌐 r/learnpython
3
1
November 13, 2022
python - Index a 2D Numpy array with 2 lists of indices - Stack Overflow
I've got a strange situation. I have a 2D Numpy array, x: x = np.random.random_integers(0,5,(20,8)) And I have 2 indexers--one with indices for the rows, and one with indices for the column. In More on stackoverflow.com
🌐 stackoverflow.com
python - 2D array indexing - Stack Overflow
How can I do the indexing of some arrays used as indices? I have the following 2D array like this: More on stackoverflow.com
🌐 stackoverflow.com
🌐
NumPy
numpy.org β€Ί devdocs β€Ί user β€Ί basics.indexing.html
Indexing on ndarrays β€” NumPy v2.6.dev0 Manual
So note that x[0, 2] == x[0][2] though the second case is more inefficient as a new temporary array is created after the first index that is subsequently indexed by 2. ... NumPy uses C-order indexing. That means that the last index usually represents the most rapidly changing memory location, unlike Fortran or IDL, where the first index represents the most rapidly changing location in memory. This difference represents a great potential for confusion. Basic slicing extends Python’s basic concept of slicing to N dimensions.
🌐
Python Like You Mean It
pythonlikeyoumeanit.com β€Ί Module3_IntroducingNumpy β€Ί AccessingDataAlongMultipleDimensions.html
Accessing Data Along Multiple Dimensions in an Array β€” Python Like You Mean It
# providing two numbers to access an element # in a 2D-array >>> grades[1, 0] # Brad's score on Exam 1 84 # negative indices work as with lists/tuples/strings >>> grades[-2, 0] # Brad's score on Exam 1 84 Β· We can also uses slices to access subsequences of our data. 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:
🌐
Reddit
reddit.com β€Ί r/learnpython β€Ί help finding the index of a given value within a 2d array
r/learnpython on Reddit: Help finding the index of a given value within a 2D array
November 13, 2022 -

Been messing around with numpy, trying to familiarize myself with it and seeing how I'd be able to utilize its arrays to store (very simple) map data for this text adventure game I've been working on. So far it seems like it'd be pretty darn useful, but I seem to have run into something of an issue.

My code is as follows (note: this is purposefully made with out a main class, because I am just trying to get the basic functionality down in my head before I incorporate it into my main program, and this is just easier for me):

# import numpy module as np
import numpy as np

# Establish the game map, a 3x3 grid of 0's
mainarray = np.array([[0, 0, 0],
                      [0, 0, 0],
                      [0, 0, 0]])

# Give feedback to make console easier to read
print(mainarray)
print("")
print("Placing player in center...")

# Place player on map (represented by a value of 1)
mainarray[1, 1] = 1

print(mainarray)

print("")
print("Locating player...")

# Attempt to find what the current index is of the value 1
print("")
print("Player is at index: ", np.where(mainarray == 1)[0][0])

In my head, I would like to eventually use this np.where() function (if I can) in one of the functions that moves my character. What I want to do is grab the current index of the "player" (represented by the number 1) and attempt to change that value to a 0, and change the value at an adjacent index to 1 (the tile that the player is moving to). Basically, I would like to use each index of the array as a sort of coordinate that I can take and use to move this "1" around the array, setting each index back to "0" after moving the "1", all based off user input.

Anyways, I am not getting any errors, however, the result that gets printed to the console is:

Player is at index:  1

Why is it just one number? It remains the same, even when I remove that second [0] in the last line. I don't fully understand how this function works, as this is the first time I've tried to use it, but since there are no errors, I have no clue what is going on.

Shouldn't I be receiving an index with two values, one for the column and one for the row? If not, how can I grab that as a result and use it in the way I described above? Is it even possible, or am I barking up the wrong tree with this function?

Find elsewhere
🌐
Guru99
guru99.com β€Ί home β€Ί python β€Ί python 2d arrays: two-dimensional list examples
Python 2D Arrays: Two-Dimensional List Examples
July 10, 2026 - The examples below show how to create, access, insert, update, delete, and size 2D arrays. 🧱 Structure: A 2D array is an array of arrays in rows and columns. πŸ”’ Indexing: Access an element with array[row][column]; indexes start at 0.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί python-using-2d-arrays-lists-the-right-way
Using 2D arrays/lists in Python - GeeksforGeeks
Python creates only one inner list and one 0 object, not separate copies. This shared reference behavior is known as shallow copying (aliasing). If we assign the 0th index to another integer say 1, then a new integer object is created with the value of 1 and then the 0th index now points to this new int object as shown below Β· Similarly, when we create a 2d array as "arr = [[0]*cols]*rows" we are essentially extending the above analogy.
Published: December 20, 2025
Top answer
1 of 5
60

Selections or assignments with np.ix_ using indexing or boolean arrays/masks

1. With indexing-arrays

A. Selection

We can use np.ix_ to get a tuple of indexing arrays that are broadcastable against each other to result in a higher-dimensional combinations of indices. So, when that tuple is used for indexing into the input array, would give us the same higher-dimensional array. Hence, to make a selection based on two 1D indexing arrays, it would be -

x_indexed = x[np.ix_(row_indices,col_indices)]

B. Assignment

We can use the same notation for assigning scalar or a broadcastable array into those indexed positions. Hence, the following works for assignments -

x[np.ix_(row_indices,col_indices)] = # scalar or broadcastable array

2. With masks

We can also use boolean arrays/masks with np.ix_, similar to how indexing arrays are used. This can be used again to select a block off the input array and also for assignments into it.

A. Selection

Thus, with row_mask and col_mask boolean arrays as the masks for row and column selections respectively, we can use the following for selections -

x[np.ix_(row_mask,col_mask)]

B. Assignment

And the following works for assignments -

x[np.ix_(row_mask,col_mask)] = # scalar or broadcastable array

Sample Runs

1. Using np.ix_ with indexing-arrays

Input array and indexing arrays -

In [221]: x
Out[221]: 
array([[17, 39, 88, 14, 73, 58, 17, 78],
       [88, 92, 46, 67, 44, 81, 17, 67],
       [31, 70, 47, 90, 52, 15, 24, 22],
       [19, 59, 98, 19, 52, 95, 88, 65],
       [85, 76, 56, 72, 43, 79, 53, 37],
       [74, 46, 95, 27, 81, 97, 93, 69],
       [49, 46, 12, 83, 15, 63, 20, 79]])

In [222]: row_indices
Out[222]: [4, 2, 5, 4, 1]

In [223]: col_indices
Out[223]: [1, 2]

Tuple of indexing arrays with np.ix_ -

In [224]: np.ix_(row_indices,col_indices) # Broadcasting of indices
Out[224]: 
(array([[4],
        [2],
        [5],
        [4],
        [1]]), array([[1, 2]]))

Make selections -

In [225]: x[np.ix_(row_indices,col_indices)]
Out[225]: 
array([[76, 56],
       [70, 47],
       [46, 95],
       [76, 56],
       [92, 46]])

As suggested by OP, this is in effect same as performing old-school broadcasting with a 2D array version of row_indices that has its elements/indices sent to axis=0 and thus creating a singleton dimension at axis=1 and thus allowing broadcasting with col_indices. Thus, we would have an alternative solution like so -

In [227]: x[np.asarray(row_indices)[:,None],col_indices]
Out[227]: 
array([[76, 56],
       [70, 47],
       [46, 95],
       [76, 56],
       [92, 46]])

As discussed earlier, for the assignments, we simply do so.

Row, col indexing arrays -

In [36]: row_indices = [1, 4]

In [37]: col_indices = [1, 3]

Make assignments with scalar -

In [38]: x[np.ix_(row_indices,col_indices)] = -1

In [39]: x
Out[39]: 
array([[17, 39, 88, 14, 73, 58, 17, 78],
       [88, -1, 46, -1, 44, 81, 17, 67],
       [31, 70, 47, 90, 52, 15, 24, 22],
       [19, 59, 98, 19, 52, 95, 88, 65],
       [85, -1, 56, -1, 43, 79, 53, 37],
       [74, 46, 95, 27, 81, 97, 93, 69],
       [49, 46, 12, 83, 15, 63, 20, 79]])

Make assignments with 2D block(broadcastable array) -

In [40]: rand_arr = -np.arange(4).reshape(2,2)

In [41]: x[np.ix_(row_indices,col_indices)] = rand_arr

In [42]: x
Out[42]: 
array([[17, 39, 88, 14, 73, 58, 17, 78],
       [88,  0, 46, -1, 44, 81, 17, 67],
       [31, 70, 47, 90, 52, 15, 24, 22],
       [19, 59, 98, 19, 52, 95, 88, 65],
       [85, -2, 56, -3, 43, 79, 53, 37],
       [74, 46, 95, 27, 81, 97, 93, 69],
       [49, 46, 12, 83, 15, 63, 20, 79]])

2. Using np.ix_ with masks

Input array -

In [19]: x
Out[19]: 
array([[17, 39, 88, 14, 73, 58, 17, 78],
       [88, 92, 46, 67, 44, 81, 17, 67],
       [31, 70, 47, 90, 52, 15, 24, 22],
       [19, 59, 98, 19, 52, 95, 88, 65],
       [85, 76, 56, 72, 43, 79, 53, 37],
       [74, 46, 95, 27, 81, 97, 93, 69],
       [49, 46, 12, 83, 15, 63, 20, 79]])

Input row, col masks -

In [20]: row_mask = np.array([0,1,1,0,0,1,0],dtype=bool)

In [21]: col_mask = np.array([1,0,1,0,1,1,0,0],dtype=bool)

Make selections -

In [22]: x[np.ix_(row_mask,col_mask)]
Out[22]: 
array([[88, 46, 44, 81],
       [31, 47, 52, 15],
       [74, 95, 81, 97]])

Make assignments with scalar -

In [23]: x[np.ix_(row_mask,col_mask)] = -1

In [24]: x
Out[24]: 
array([[17, 39, 88, 14, 73, 58, 17, 78],
       [-1, 92, -1, 67, -1, -1, 17, 67],
       [-1, 70, -1, 90, -1, -1, 24, 22],
       [19, 59, 98, 19, 52, 95, 88, 65],
       [85, 76, 56, 72, 43, 79, 53, 37],
       [-1, 46, -1, 27, -1, -1, 93, 69],
       [49, 46, 12, 83, 15, 63, 20, 79]])

Make assignments with 2D block(broadcastable array) -

In [25]: rand_arr = -np.arange(12).reshape(3,4)

In [26]: x[np.ix_(row_mask,col_mask)] = rand_arr

In [27]: x
Out[27]: 
array([[ 17,  39,  88,  14,  73,  58,  17,  78],
       [  0,  92,  -1,  67,  -2,  -3,  17,  67],
       [ -4,  70,  -5,  90,  -6,  -7,  
2 of 5
12

What about:

x[row_indices][:,col_indices]

For example,

x = np.random.random_integers(0,5,(5,5))
## array([[4, 3, 2, 5, 0],
##        [0, 3, 1, 4, 2],
##        [4, 2, 0, 0, 3],
##        [4, 5, 5, 5, 0],
##        [1, 1, 5, 0, 2]])

row_indices = [4,2]
col_indices = [1,2]
x[row_indices][:,col_indices]
## array([[1, 5],
##        [2, 0]])
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]])
🌐
Quora
quora.com β€Ί How-do-I-return-the-index-of-an-element-in-a-2D-array-in-Python
How to return the index of an element in a 2D array in Python - Quora
Answer (1 of 2): You don’t return the index - your code needs to keep track of where the elements are. If you are asking how do you search a 2D array - the only way is something like this: [code]def find_all(matrix, element): """Iterate through all row, column indexes in a 2D Matrix where ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί indexing-multi-dimensional-arrays-in-python-using-numpy
Indexing Multi-dimensional arrays in Python using NumPy | GeeksforGeeks
April 28, 2025 - By default, it is a 2d array. ... To index a multi-dimensional array you can index with a slicing operation similar to a single dimension array. ... import numpy as np arr_m = np.arange(12).reshape(2, 2, 3) # Indexing print(arr_m[0:3]) print() ...
🌐
Snakify
snakify.org β€Ί two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
In this array there are n = 5 rows, m = 6 columns, and the element with row index i and column index j is calculated by the formula a[i][j] = i * j.
🌐
Pluralsight
pluralsight.com β€Ί blog β€Ί tech guides & tutorials
Working with Numpy Arrays: Indexing & Slicing | Pluralsight
November 2, 2018 - One for the row and the other for the column. 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.
🌐
Programiz
programiz.com β€Ί python-programming β€Ί numpy β€Ί array-indexing
Numpy Array Indexing (With Examples)
In NumPy, we can access specific rows or columns of a 2-D array using array indexing. Let's see an example. import numpy as np # create a 2D array array1 = np.array([[1, 3, 5], [7, 9, 2], [4, 6, 8]]) # access the second row of the array second_row = array1[1, :] print("Second Row:", second_row) # Output: [7 9 2] # access the third column of the array third_col = array1[:, 2] print("Third Column:", third_col) # Output: [5 2 8]
🌐
Javatpoint
javatpoint.com β€Ί python-2d-array
Python 2D array - Javatpoint
January 10, 2021 - Python 2D array with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.
🌐
Uni-heidelberg
ita.uni-heidelberg.de β€Ί ~dullemond β€Ί lectures β€Ί python_2019 β€Ί py4sci_wed β€Ί Note on IndexOrdering.html
Index ordering in Numpy
Now things get really confusing! As you see, the index order is now: y,x,z, while the argument order to meshgrid() remains x,y,z. Therefore, for 3-D and higher-dimensional arrays, I recommend always to use indexing='ij'!
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python-using-2d-arrays-lists-the-right-way
Python | Using 2D arrays/lists the right way - GeeksforGeeks
In method 1a, Python doesn't create 5 integer objects but creates only one integer object, and all the indices of the array arr point to the same int object as shown. If we assign the 0th index to another integer say 1, then a new integer object is created with the value of 1 and then the 0th index now points to this new int object as shown below Β· Similarly, when we create a 2d array as "arr = [[0]*cols]*rows" we are essentially extending the above analogy.
Published: June 20, 2024
🌐
Drbeane
drbeane.github.io β€Ί python_dsci β€Ί pages β€Ί array_2d.html
2-Dimensional Arrays β€” Python for Data Science
The first number indexes a row in the array and the second number indexes a column. ... We can also use slicing with 2D arrays.