To answer this question, we have to look at how indexing a multidimensional array works in Numpy. Let's first say you have the array x from your question. The buffer assigned to x will contain 16 ascending integers from 0 to 15. If you access one element, say x[i,j], NumPy has to figure out the memory location of this element relative to the beginning of the buffer. This is done by calculating in effect i*x.shape[1]+j (and multiplying with the size of an int to get an actual memory offset).

If you extract a subarray by basic slicing like y = x[0:2,0:2], the resulting object will share the underlying buffer with x. But what happens if you acces y[i,j]? NumPy can't use i*y.shape[1]+j to calculate the offset into the array, because the data belonging to y is not consecutive in memory.

NumPy solves this problem by introducing strides. When calculating the memory offset for accessing x[i,j], what is actually calculated is i*x.strides[0]+j*x.strides[1] (and this already includes the factor for the size of an int):

x.strides
(16, 4)

When y is extracted like above, NumPy does not create a new buffer, but it does create a new array object referencing the same buffer (otherwise y would just be equal to x.) The new array object will have a different shape then x and maybe a different starting offset into the buffer, but will share the strides with x (in this case at least):

y.shape
(2,2)
y.strides
(16, 4)

This way, computing the memory offset for y[i,j] will yield the correct result.

But what should NumPy do for something like z=x[[1,3]]? The strides mechanism won't allow correct indexing if the original buffer is used for z. NumPy theoretically could add some more sophisticated mechanism than the strides, but this would make element access relatively expensive, somehow defying the whole idea of an array. In addition, a view wouldn't be a really lightweight object anymore.

This is covered in depth in the NumPy documentation on indexing.

Oh, and nearly forgot about your actual question: Here is how to make the indexing with multiple lists work as expected:

x[[[1],[3]],[1,3]]

This is because the index arrays are broadcasted to a common shape. Of course, for this particular example, you can also make do with basic slicing:

x[1::2, 1::2]
Answer from Sven Marnach on Stack Overflow
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_slicing.asp
NumPy Array Slicing
Slice elements from index 4 to the end of the array: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[4:]) Try it Yourself »
Top answer
1 of 7
122

To answer this question, we have to look at how indexing a multidimensional array works in Numpy. Let's first say you have the array x from your question. The buffer assigned to x will contain 16 ascending integers from 0 to 15. If you access one element, say x[i,j], NumPy has to figure out the memory location of this element relative to the beginning of the buffer. This is done by calculating in effect i*x.shape[1]+j (and multiplying with the size of an int to get an actual memory offset).

If you extract a subarray by basic slicing like y = x[0:2,0:2], the resulting object will share the underlying buffer with x. But what happens if you acces y[i,j]? NumPy can't use i*y.shape[1]+j to calculate the offset into the array, because the data belonging to y is not consecutive in memory.

NumPy solves this problem by introducing strides. When calculating the memory offset for accessing x[i,j], what is actually calculated is i*x.strides[0]+j*x.strides[1] (and this already includes the factor for the size of an int):

x.strides
(16, 4)

When y is extracted like above, NumPy does not create a new buffer, but it does create a new array object referencing the same buffer (otherwise y would just be equal to x.) The new array object will have a different shape then x and maybe a different starting offset into the buffer, but will share the strides with x (in this case at least):

y.shape
(2,2)
y.strides
(16, 4)

This way, computing the memory offset for y[i,j] will yield the correct result.

But what should NumPy do for something like z=x[[1,3]]? The strides mechanism won't allow correct indexing if the original buffer is used for z. NumPy theoretically could add some more sophisticated mechanism than the strides, but this would make element access relatively expensive, somehow defying the whole idea of an array. In addition, a view wouldn't be a really lightweight object anymore.

This is covered in depth in the NumPy documentation on indexing.

Oh, and nearly forgot about your actual question: Here is how to make the indexing with multiple lists work as expected:

x[[[1],[3]],[1,3]]

This is because the index arrays are broadcasted to a common shape. Of course, for this particular example, you can also make do with basic slicing:

x[1::2, 1::2]
2 of 7
70

As Sven mentioned, x[[[0],[2]],[1,3]] will give back the 0 and 2 rows that match with the 1 and 3 columns while x[[0,2],[1,3]] will return the values x[0,1] and x[2,3] in an array.

There is a helpful function for doing the first example I gave, numpy.ix_. You can do the same thing as my first example with x[numpy.ix_([0,2],[1,3])]. This can save you from having to enter in all of those extra brackets.

Discussions

Slicing 2D Numpy arrays
You have to understand slices: they have the structure start:stop:step, where start tells you the index from where you'll get the values; stop tells you the next to last index for the values (meaning that your last index will be the previous one, not this), and step will give you the distance between elements. So, 1:2 tells you from the row 1 to the row 2 without including row 2. That gives you just row 1. More on reddit.com
🌐 r/PythonLearning
9
3
August 11, 2024
NumPy 2D array slicing. Is it possible?

I can’t say I’d recommend this, but technically it is only one index…

r, c = data.shape
data[[*[0]*c,*[*range(1,r-1)]*2,*[r-1]*c],[*range(c),*np.repeat([0,c-1],r-2),*range(c)]] = 0

A slightly nicer approach would be

data[np.pad(np.zeros(np.array(data.shape)-2,dtype=bool),1,constant_values=1)] = 0

An alternative approach would be to just replace the variable with the padded internals

data = np.pad(data[-1:1,-1:1], 1, constant_values=0)
More on reddit.com
🌐 r/learnpython
2
0
February 9, 2023
Problems with slicing 2D array
0, is valid syntax which represents a tuple so probably you should try removing the commas. More on reddit.com
🌐 r/learnpython
5
4
December 8, 2022
Numpy array slicing view vs copy confusion
Indexing a NumPy array does indeed give a view if it is basic indexing (so indices that are either scalar integers or slice objects). Advanced indexing (using boolean or integer arrays as indices) returns a copy. Assuming you are referring to your assignments to puffArray, they should (and do in my tests) reflect also on areaArray, provided xIndex and yIndex work out such that the radius check succeeds, of course. Though I am not quite following what you are doing conceptually, that is where I believe your issue may lay: Note that you are calculating the distance between areaIter.multi_index, which is in the coordinate system of the 401x401 slice, and [xIndex, yIndex] which is in the coordinate system of the full areaArray. So for many [xIndex, yIndex], radius <= 200 is never going to be true. More on reddit.com
🌐 r/learnpython
5
7
August 3, 2021
🌐
StrataScratch
stratascratch.com › blog › numpy-array-slicing-in-python
NumPy Array Slicing in Python - StrataScratch
March 1, 2024 - Here is what we will do: The matrix[1, :] slice selects all elements in the second row, showing how to slice rows. The matrix[:, 2] slice selects all elements in the third column, demonstrating column slicing.
🌐
Delft Stack
delftstack.com › home › howto › numpy › python numpy slice 2d array
How to Slice 2D Array in NumPy | Delft Stack
February 2, 2024 - In the above code, we extracted the elements in rows 1 and 3 that intersect with columns 1 and 3 while skipping row 2 and column 2 with the array indexing method in Python. This can also be done with a similar approach but with some different syntax, as shown in the coding example below. import numpy as np x = range(16) x = np.reshape(x, (4, 4)) print(x) y = x[0::2, 1::2] print(y)
🌐
Statology
statology.org › home › how to slice a 2d numpy array (with examples)
How to Slice a 2D NumPy Array (With Examples)
January 24, 2023 - Note that the syntax 1:3 tells NumPy to select columns 1 up to 3, but doesn’t include 3. Thus, this syntax selects all of the values in the columns with index positions of 1 and 2. We can use the following syntax to select the rows in index positions 2 through 5 and the columns in index positions 1 through 3: #select rows in 2:5 and columns in 1:3 arr[2:5, 1:3] array([[ 9, 10], [13, 14], [17, 18]]) This syntax returns all of the values in the 2D NumPy array between row index positions 2 through 5 and column index positions 1 through 3.
🌐
Reddit
reddit.com › r/pythonlearning › slicing 2d numpy arrays
r/PythonLearning on Reddit: Slicing 2D Numpy arrays
August 11, 2024 -

So,here I come again 🤣

I don't get slicing in 2D..

In my lesson,I was taught that using this

d[1:2,1] 

means the 2nd element from the last two rows,and 2nd element from 1st column should be sliced..but when I use it I get only one element.Did I do something wrong?Can some of you awesome people hook me up with an explanation?

Here's some code for your palates:

a=[[1,2,3],[4,5,6],[7,8,9]]
import numpy as np
d=np.array(a)
d[1:2,1]
🌐
Turing
turing.com › kb › guide-to-numpy-array-slicing
A Useful Guide to NumPy Array Slicing
To slice a 2-D array in NumPy, you have to specify row index and column index which are separated using a comma as shown below.
Find elsewhere
🌐
Earth Data Science
earthdatascience.org › home
Slice (or Select) Data From Numpy Arrays | Earth Data Science - Earth Lab
September 23, 2019 - Numpy arrays are an efficient data structure for working with scientific data in Python. Learn how to use indexing to slice (or select) data from one-dimensional and two-dimensional numpy arrays.
🌐
DataCamp
datacamp.com › doc › numpy › array-slicing
NumPy Array Slicing
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) slice_arr = arr[0:2, 1:3] # slice_arr yields [[2, 3], [5, 6]] This example slices a 2D array to yield [[2, 3], [5, 6]], capturing elements from the first two rows and the last two columns.
🌐
GeeksforGeeks
geeksforgeeks.org › python › slice-a-2d-array-in-python
Slice a 2D Array in Python - GeeksforGeeks
July 23, 2025 - In this example, np.split() is used with the axis=1 parameter to split the 2D array along the columns. The second argument [1, 2] specifies the indices at which the array should be split.
🌐
Programiz
programiz.com › python-programming › numpy › array-slicing
NumPy Array Slicing (With Examples)
import numpy as np # create a 2D array array1 = np.array([[1, 3, 5, 7], [9, 11, 13, 15], [2, 4, 6, 8]]) # slice the array to get the first two rows and columns subarray1 = array1[:2, :2] # slice the array to get the last two rows and columns subarray2 = array1[1:3, 2:4] # print the subarrays print("First Two Rows and Columns: \n",subarray1) print("Last two Rows and Columns: \n",subarray2)
🌐
Problem Solving with Python
problemsolvingwithpython.com › 05-NumPy-and-Arrays › 05.06-Array-Slicing
Array Slicing - Problem Solving with Python
2D NumPy arrays can be sliced with the general form: <slice> = <array>[start_row:end_row, start_col:end_col] The code section below creates a two row by four column array and indexes out the first two rows and the first three columns. In [5]: import numpy as np ·
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-slicing-multi-dimensional-arrays
Python Slicing Multi-Dimensional Arrays - GeeksforGeeks
July 1, 2026 - import numpy as np arr = np.array([ ... the last column values from every matrix. Slices can be used not only for reading data but also for updating multiple values at once....
🌐
Pythoninformer
pythoninformer.com › python-libraries › numpy › index-and-slice
PythonInformer - Indexing and slicing numpy arrays
February 4, 2018 - You can slice a numpy array is a similar way to slicing a list - except you can do it in more than one dimension.
🌐
APXML
apxml.com › courses › essential-numpy-pandas › chapter-3-numpy-array-indexing-slicing › slicing-2d-arrays
Slice 2D NumPy Arrays - ApX Machine Learning
# Create a copy of a slice slice_copy = arr2d[2:, 2:].copy() print("\nSlice copy:\n", slice_copy) # Modify the copy slice_copy[0, 0] = -1 print("\nModified slice copy:\n", slice_copy) # Check the original array again - it remains unchanged in this region print("\nOriginal array after modifying copy:\n", arr2d) Mastering 2D slicing is essential for manipulating tabular data, image patches, or any grid-like structure efficiently within NumPy.
🌐
NumPy
numpy.org › doc › stable › user › basics.indexing.html
Indexing on ndarrays — NumPy v2.5 Manual
The standard rules of sequence slicing apply to basic slicing on a per-dimension basis (including using a step index). Some useful concepts to remember include: 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\)).
🌐
NumPy
numpy.org › devdocs › user › basics.indexing.html
Indexing on ndarrays — NumPy v2.6.dev0 Manual
The standard rules of sequence slicing apply to basic slicing on a per-dimension basis (including using a step index). Some useful concepts to remember include: 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\)).
🌐
Codegive
codegive.com › blog › numpy_array_slicing_in_python.php
Numpy array slicing in python
# Select elements from rows 1 to 3 AND columns 1 to 3 print("\nSub-array from rows 1:4 and cols 1:4 (arr_2d[1:4, 1:4]):\n", arr_2d[1:4, 1:4]) You can combine single indices (which reduce the dimension) with slices.
🌐
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
We can select these two with x[1:]. As both of the rows are the first row of its corresponding two-dimensional array, row index is zero. ... Slice through both columns and rows and print part of first two rows of the last two two-dimensional arrays
🌐
Codefinity
codefinity.com › courses › v2 › 4f4826d5-e2f8-4ffd-9fd0-6f513353d70a › c7208d3b-40d2-4c59-af68-d2f17b36729b › 6059a206-b008-4b87-912d-4fec9acec4f9
Learn Slicing in 2D Arrays | Indexing and Slicing
If you want to perform slicing ... on the elements of these 1D arrays (axis 1), the syntax is as follows: array[start:end:step, start:end:step]. Essentially, the number of slices corresponds to the number of dimensions of ...