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
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[4:]) Try it Yourself » · Slice elements from the beginning to index 4 (not included):
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
python - Slice 2d array into smaller 2d arrays - Stack Overflow
For now it just works when the big 2d array can be perfectly sliced into equally sized subarrays. More on stackoverflow.com
🌐 stackoverflow.com
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
Is it possible to transpose a 2d array with slicing?
Grab all the 0th elements, then all the 1st, then all the 2nd... Alternatively: T = list(zip(*A)) More on reddit.com
🌐 r/learnpython
4
1
January 29, 2021
🌐
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.
🌐
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]
🌐
NumPy
numpy.org › doc › stable › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.4 Manual
Like the original list, the array is mutable. ... Also like the original list, Python slice notation can be used for indexing.
🌐
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.
🌐
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.
Find elsewhere
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › tech guides & tutorials
Working with Numpy Arrays: Indexing & Slicing | Pluralsight
Slicing a two-dimensional array is very similar to slicing a one-dimensional array. You just use a comma to separate the row slice and the column slice. For example: ... Similarly, you can extend this for higher dimensional arrays.
🌐
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)
🌐
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.
🌐
NumPy
numpy.org › devdocs › user › basics.indexing.html
Indexing on ndarrays — NumPy v2.5.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\)).
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-slicing-multi-dimensional-arrays
Python slicing multi-dimensional arrays - GeeksforGeeks
July 23, 2025 - In this example, we first create a 3-D NumPy array called array_3d. Then, we use negative indexing to slice the last row from each 2-D matrix within the 3-D array.
🌐
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 we want to perform slicing only ... 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 ...
Top answer
1 of 12
110

There was another question a couple of months ago which clued me in to the idea of using reshape and swapaxes. The h//nrows makes sense since this keeps the first block's rows together. It also makes sense that you'll need nrows and ncols to be part of the shape. -1 tells reshape to fill in whatever number is necessary to make the reshape valid. Armed with the form of the solution, I just tried things until I found the formula that works.

You should be able to break your array into "blocks" using some combination of reshape and swapaxes:

def blockshaped(arr, nrows, ncols):
    """
    Return an array of shape (n, nrows, ncols) where
    n * nrows * ncols = arr.size

    If arr is a 2D array, the returned array should look like n subblocks with
    each subblock preserving the "physical" layout of arr.
    """
    h, w = arr.shape
    assert h % nrows == 0, f"{h} rows is not evenly divisible by {nrows}"
    assert w % ncols == 0, f"{w} cols is not evenly divisible by {ncols}"
    return (arr.reshape(h//nrows, nrows, -1, ncols)
               .swapaxes(1,2)
               .reshape(-1, nrows, ncols))

turns c

np.random.seed(365)
c = np.arange(24).reshape((4, 6))
print(c)

[out]:
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]

into

print(blockshaped(c, 2, 3))

[out]:
[[[ 0  1  2]
  [ 6  7  8]]

 [[ 3  4  5]
  [ 9 10 11]]

 [[12 13 14]
  [18 19 20]]

 [[15 16 17]
  [21 22 23]]]

I've posted an inverse function, unblockshaped, here, and an N-dimensional generalization here. The generalization gives a little more insight into the reasoning behind this algorithm.


Note that there is also superbatfish's blockwise_view. It arranges the blocks in a different format (using more axes) but it has the advantage of (1) always returning a view and (2) being capable of handling arrays of any dimension.

2 of 12
8

It seems to me that this is a task for numpy.split or some variant.

e.g.

a = np.arange(30).reshape([5,6])  #a.shape = (5,6)
a1 = np.split(a,3,axis=1) 
#'a1' is a list of 3 arrays of shape (5,2)
a2 = np.split(a, [2,4])
#'a2' is a list of three arrays of shape (2,5), (2,5), (1,5)

If you have a NxN image you can create, e.g., a list of 2 NxN/2 subimages, and then divide them along the other axis.

numpy.hsplit and numpy.vsplit are also available.

🌐
Mdjubayerhossain
mdjubayerhossain.com › numpy › notebooks › 04_ArraySlicingandSubsetting.html
Array Indexing and Slicing — Introduction to NumPy
# Create a 2D array of students info students = np.array([['Alice','Beth','Cathy','Dorothy'], [65,78,90,81], [71,82,79,92]]) ... Slicing can also include ellipsis (…) to make a selection tuple of the same length as the dimension of an array. The dots (…) represent as many colons as needed to produce a complete indexing tuple ... NumPy arrays can be indexed with slices...
🌐
Earth Data Science
earthdatascience.org › home
Slice (or Select) Data From Numpy Arrays | Earth Data Science - Earth Lab
September 23, 2019 - # Select the last element of the array avg_monthly_precip[-1] ... You can slice a range of elements from one-dimensional numpy arrays such as the third, fourth and fifth elements, by specifying an index range: [starting_value, ending_value].
🌐
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.
🌐
Canard Analytics
canardanalytics.com › blog › index-slicing-numpy-arrays
Indexing and Slicing NumPy Arrays | Canard Analytics
July 10, 2022 - Slicing is a powerful technique that forms a part of the backbone of the NumPy package. In this tutorial we covered the following concepts: Discussed array indexing and indexing of multidimensional arrays. We looked at slicing of arrays and how the method is very similar to slicing Python lists. We highlighted that slicing an array returns a view or shallow copy of the original array, which means that any changes made to values in the slice will be present in the original array also.
🌐
Quora
quora.com › How-do-I-slice-a-2D-array-on-Python-without-using-NumPy
How to slice a 2D array on Python without using NumPy - Quora
Answer (1 of 2): Horizontal slicing is possible, but for vertical slicing you’ll need NumPy for it. Here’s the code and make sure you follow the comments:- [code]a = [[1,2,3],[4,5,6],[7,8,9]] #(1)Horizontal rows r1 = a[0][:] r2 = a[1][:] r3 = a[2][:] #(2)For specific value required slicing ...
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › numpy array slicing
NumPy Array Slicing - Spark By {Examples}
March 27, 2024 - Slicing a 2D NumPy array involves specifying ranges for both rows and columns. For example, arr[0:2, 1:3] extracts the subarray consisting of rows 0 to 1 and columns 1 to 2. ... You can reverse a NumPy array using slicing by specifying a step ...