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
NumPy Editor NumPy Quiz NumPy Exercises NumPy Syllabus NumPy Study Plan NumPy Certificate ... Slicing in python means taking elements from one given index to another given index. We pass slice instead of index like this: [start:end]. We can ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.4 Manual
Basic slicing occurs when obj is a slice object (constructed by start:stop:step notation inside of brackets), an integer, or a tuple of slice objects and integers. Ellipsis and newaxis objects can be interspersed with these as well. The simplest case of indexing with N integers returns an array ...
Discussions

python - Slicing of a NumPy 2d array, or how do I extract an mxm submatrix from an nxn array (n>m)? - Stack Overflow
I want to slice a NumPy nxn array. I want to extract an arbitrary selection of m rows and columns of that array (i.e. without any pattern in the numbers of rows/columns), making it a new, mxm array... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Understanding the slicing of NumPy array - Stack Overflow
For the above notation, the slice will include the elements starting at dimX_start, up to, and not including, dimX_end. ... I hope that helps explain your results. For the result you were expecting, you would need something more like: ... For more info on slicing, you might go to the numpy docs ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Numpy array slicing using colons - Stack Overflow
I am trying to learn numpy array slicing. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Slicing multiple, non-contiguous rows and columns from a numpy array or matrix
If you just want to have a new array that is 2nd and 4th rows and columns you can do this: b = np.array([[a[1][1], a[1][3]], [a[3][1], a[3][3]]]) It's not very elegant, but I think for more elegant, we would need a more detailed description of the problem you are facing. More on reddit.com
๐ŸŒ r/learnpython
11
5
April 21, 2015
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ numpy-slicing-and-indexing
Basic Slicing and Advanced Indexing in NumPy - GeeksforGeeks
Explanation: arr[[3, 1, 2]] creates a new array by picking elements from indices 3, 1 and 2 of the original array, resulting in [4, 8, 6]. Basic slicing and indexing are used to access specific elements or a range of elements from a NumPy array.
Published ย  November 4, 2025
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ array-slicing
NumPy Array Slicing
The slicing syntax follows the format array[start:stop:step], where start is the index to begin the slice, stop is the index to end the slice (exclusive), and step defines the interval between elements. import numpy as np # Syntax array_slice = array[start:stop:step]
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.

๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.3 โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.3 Manual
Basic slicing occurs when obj is a slice object (constructed by start:stop:step notation inside of brackets), an integer, or a tuple of slice objects and integers. Ellipsis and newaxis objects can be interspersed with these as well. The simplest case of indexing with N integers returns an array ...
Find elsewhere
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ numpy-basics โ€บ lessons โ€บ array-indexing-and-slicing-in-numpy
Array Indexing and Slicing in NumPy - Python
It works just like with Python's lists! Python uses zero-based indexing, meaning the first element is at position 0. Here's how we access elements: Note that [-1] gives us the last element, the same as with plain Python's lists! ... Array slicing lets us access a subset, or slice, of an array.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ array-slicing
NumPy Array Slicing (With Examples)
Array Slicing is the process of extracting a portion of an array.Array Slicing is the process of extracting a portion of an array. With slicing, we can easily access elements in the array. It can be done on one or more dimensions of a NumPy array. Syntax of NumPy Array Slicing Here's the syntax ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ absolute_beginners.html
NumPy: the absolute basics for beginners โ€” NumPy v2.4 Manual
You can also expand an array by inserting a new axis at a specified position with np.expand_dims. ... Find more information about newaxis here and expand_dims at expand_dims. You can index and slice NumPy arrays in the same ways you can slice Python lists.
Top answer
1 of 3
16

The commas in slicing are to separate the various dimensions you may have. In your first example you are reshaping the data to have 4 dimensions each of length 2. This may be a little difficult to visualize so if you start with a 2D structure it might make more sense:

>>> a = np.arange(16).reshape((4, 4))
>>> a
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11],
           [12, 13, 14, 15]])
>>> a[0]  # access the first "row" of data
    array([0, 1, 2, 3])
>>> a[0, 2]  # access the 3rd column (index 2) in the first row of the data
    2

If you want to access multiple values using slicing you can use the colon to express a range:

>>> a[:, 1]  # get the entire 2nd (index 1) column
    array([[1, 5, 9, 13]])
>>> a[1:3, -1]  # get the second and third elements from the last column
    array([ 7, 11])
>>> a[1:3, 1:3]  # get the data in the second and third rows and columns
    array([[ 5,  6],
           [ 9, 10]])

You can do steps too:

>>> a[::2, ::2]  # get every other element (column-wise and row-wise)
    array([[ 0,  2],
          [ 8, 10]])

Hope that helps. Once that makes more sense you can look in to stuff like adding dimensions by using None or np.newaxis or using the ... ellipsis:

>>> a[:, None].shape
    (4, 1, 4)

You can find more here: http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

2 of 3
4

It might pay to explore the shape and individual entries as we go along.

Let's start with

>>> a = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
>>> a.shape
(16, )

This is a one-dimensional array of length 16.

Now let's try

>>> a = a.reshape(2,2,2,2)
>>> a.shape
(2, 2, 2, 2)

It's a multi-dimensional array with 4 dimensions.

Let's see the 0, 1 element:

>>> a[0, 1]
array([[5, 6],
   [7, 8]])

Since there are two dimensions left, it's a matrix of two dimensions.


Now a[:, 1] says: take a[i, 1 for all possible values of i:

>>> a[:, 1]
array([[[ 5,  6],
    [ 7,  8]],

   [[13, 14],
    [15, 16]]])

It gives you an array where the first item is a[0, 1], and the second item is a[1, 1].

๐ŸŒ
StrataScratch
stratascratch.com โ€บ blog โ€บ numpy-array-slicing-in-python
NumPy Array Slicing in Python - StrataScratch
March 1, 2024 - Then, we slice this array from index 1 to index 5 (inclusive of 1 and exclusive of 6), resulting in a new array sliced_arr that contains the elements [1 2 3 4 5]. This shows how slicing allows us to easily select a subset of an array's elements based on their indices. import numpy as np # Create a one-dimensional array arr = np.arange(10) # Array of integers from 0 to 9 print("Original array:", arr) # Slice from index 1 to index 5 sliced_arr = arr[1:6] print("Sliced array:", sliced_arr)
๐ŸŒ
Canard Analytics
canardanalytics.com โ€บ blog โ€บ index-slicing-numpy-arrays
Indexing and Slicing NumPy Arrays | Canard Analytics
July 10, 2022 - 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.
๐ŸŒ
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]
๐ŸŒ
YouTube
youtube.com โ€บ watch
Slicing in NumPy is easy! โœ‚๏ธ - YouTube
July 27, 2025 - #coding #numpy #python Slicing in NumPy allows you to extract portions of an array using a [start:stop:step] syntax. It works similarly to Python lists but ...
Top answer
1 of 3
17

You can slice and insert a new axis in one single operation. For example, here's a 2D array:

>>> a = np.arange(1, 7).reshape(2, 3)
>>> a
array([[1, 2, 3],
       [4, 5, 6]])

To slice out a single column (returning array of shape (2, 1)), slice with None as the third dimension:

>>> a[:, 1, None]
array([[2],
       [5]])

To slice out a single row (returning array of shape (1, 3)), slice with None as the second dimension:

>>> a[0, None, :]
array([[1, 2, 3]])
2 of 3
7

Make the index a slice, list or array

    X[[0],:]
    X[0:1,4]

But there's nothing wrong with reshape other than the fact that it requires typing. It isn't slow. [None,:] is a nice short hand for it.

Use of a list index may be the shortest, but it does produce a copy (a plus or minus?) and is slower

For (100,100) integer array:

In [487]: timeit x[[50],:]
100000 loops, best of 3: 10.3 ยตs per loop  # slowest

In [488]: timeit x[50:51,:]
100000 loops, best of 3: 2.24 ยตs per loop   # slice indexing is fast

In [489]: timeit x[50,:].reshape(1,-1)
100000 loops, best of 3: 3.29 ยตs per loop  # minimal time penalty

In [490]: timeit x[50,:][None,:]
100000 loops, best of 3: 3.55 ยตs per loop

In [543]: timeit x[None,50,:]          # **best**
1000000 loops, best of 3: 1.76 ยตs per loop

One test for copy is to compare the data buffer pointer with the original.

In [492]: x.__array_interface__['data']
Out[492]: (175920456, False)
In [493]: x[50,:].__array_interface__['data']
Out[493]: (175940456, False)
In [494]: x[[50],:].__array_interface__['data']
Out[494]: (175871672, False)    # different pointer
In [495]: x[50:51,:].__array_interface__['data']
Out[495]: (175940456, False)
In [496]: x[50,:][None,:].__array_interface__['data']
Out[496]: (175940456, False)
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ numpy โ€บ numpy indexing and slicing
NumPy Indexing and Slicing
March 5, 2015 - This slice object is passed to the array to extract a part of array. In the below code we will see how to access last two elements from the array using arr[-2:] as we didn't specify the stop parameter it access the elements from the second last to the end of the array. import numpy as np arr = np.arange(6) print(arr[-2:])