If you use numpy, this is easy:

slice = arr[:2,:2]

or if you want the 0's,

slice = arr[0:2,0:2]

You'll get the same result.

*note that slice is actually the name of a builtin-type. Generally, I would advise giving your object a different "name".


Another way, if you're working with lists of lists*:

slice = [arr[i][0:2] for i in range(0,2)]

(Note that the 0's here are unnecessary: [arr[i][:2] for i in range(2)] would also work.).

What I did here is that I take each desired row 1 at a time (arr[i]). I then slice the columns I want out of that row and add it to the list that I'm building.

If you naively try: arr[0:2] You get the first 2 rows which if you then slice again arr[0:2][0:2], you're just slicing the first two rows over again.

*This actually works for numpy arrays too, but it will be slow compared to the "native" solution I posted above.

Answer from mgilson on Stack Overflow
🌐
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. The resulting slices are stored in the slices variable.
Top answer
1 of 4
102

If you use numpy, this is easy:

slice = arr[:2,:2]

or if you want the 0's,

slice = arr[0:2,0:2]

You'll get the same result.

*note that slice is actually the name of a builtin-type. Generally, I would advise giving your object a different "name".


Another way, if you're working with lists of lists*:

slice = [arr[i][0:2] for i in range(0,2)]

(Note that the 0's here are unnecessary: [arr[i][:2] for i in range(2)] would also work.).

What I did here is that I take each desired row 1 at a time (arr[i]). I then slice the columns I want out of that row and add it to the list that I'm building.

If you naively try: arr[0:2] You get the first 2 rows which if you then slice again arr[0:2][0:2], you're just slicing the first two rows over again.

*This actually works for numpy arrays too, but it will be slow compared to the "native" solution I posted above.

2 of 4
2

To slice a multi-dimensional array, the dimension (i.e. axis) must be specified. As OP noted, arr[i:j][i:j] is exactly the same as arr[i:j] because arr[i:j] sliced along the first axis (rows) and has the same number of dimensions as arr (you can confirm by arr[i:j].ndim == arr.ndim); so the second slice is still slicing along the first dimension (which was already done by the first slice). To slice along the second dimension, it must be explicitly specified, e.g.:

arr[:2][:, :2]                   # its output is the same as `arr[:2, :2]`

A bare : means slice everything in that axis, so there's an implicit : for the second axis in the above code (i.e. arr[:2, :][:, :2]). What the above code is doing is slicing the first two rows (or first two arrays along the first axis) and then slice the first two columns (or the first two arrays along the second axis) from the resulting array.

An ... can be used instead of multiple colons (:), so for a general n-dimensional array, the following produce the same output:

w = arr[i:j, m:n]
x = arr[i:j, m:n, ...]
y = arr[i:j][:, m:n]
z = arr[i:j, ...][:, m:n, ...]

That said, arr[:2, :2] is the canonical way because in the case of arr[i:j][:, i:j], arr[i:j] creates a temporary array which is indexed by [:, i:j], so it's comparatively inefficient.

However, there are cases where chained indexing makes sense (or readable), e.g., if you want to index a multi-dimensional array using a list of indices. For example, if you want to slice the top-left quarter of a 4x4 array using a list of indices, then chained indexing gives the correct result whereas a single indexing gives a different result (it's because of numpy advanced indexing) where the values correspond to the index pair for each position in the index lists.

arr = np.arange(1,17).reshape(4,4)
rows = cols = [0,1]
arr[rows][:, cols]               # <--- correct output
arr[rows, cols]                  # <--- wrong output
arr[[[e] for e in rows], cols]   # <--- correct output
arr[np.ix_(rows, cols)]          # <--- correct output
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 array slicing -- How can 2D array slicing be implemented? - Stack Overflow
I am wondering how 2D array slicing can be implemented in Python? For example, arr is an instance of a self-defined class 2D array. if I want to enable 2D slicing syntax on this object like thi... More on stackoverflow.com
🌐 stackoverflow.com
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
python - Slice in 2 dimensional array - Stack Overflow
I have 2 dimensional array I want to get every first element in 2 dimensional list. But when I try to using slice I have strange behavior. More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › slice-a-2d-list-in-python
Slice a 2D List in Python - GeeksforGeeks
July 23, 2025 - Python3 · import numpy as np # ... 8]] In conclusion, slicing a 2D list in Python allows for efficient extraction of specific elements or sublists based on specified ranges....
🌐
StrataScratch
stratascratch.com › blog › numpy-array-slicing-in-python
NumPy Array Slicing in Python - StrataScratch
March 1, 2024 - import numpy as np # Create a ... 2:", slice3) Here is the output. Slicing 2D arrays in NumPy allows you to access subsets of the array's rows and columns....
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_slicing.asp
NumPy Array Slicing
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 also define the step, like this: [start:end:step]. ... Note: The result includes the start index, ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-slicing-multi-dimensional-arrays
Python Slicing Multi-Dimensional Arrays - GeeksforGeeks
July 1, 2026 - This produces a new 2-D array containing the last column values from every matrix. Slices can be used not only for reading data but also for updating multiple values at once.
🌐
Nanyang Technological University
libguides.ntu.edu.sg › python › arrayslicing
NP.4 Array Slicing - Python for Basic Data Analysis - LibGuides at Nanyang Technological University
August 26, 2026 - Array slicing is similar to list slicing in Python. Array indexing also begins from 0. However, since arrays can be multidimensional, we have to specify the slice for each dimension. As we are mainly working with 2 dimensional arrays in this guide, we need to specify the row and column like ...
Find elsewhere
🌐
Problem Solving with Python
problemsolvingwithpython.com › 05-NumPy-and-Arrays › 05.06-Array-Slicing
Array Slicing - Problem Solving with Python
Multiple values stored within an array can be accessed simultaneously with array slicing. To pull out a section or slice of an array, the colon operator : is used when calling the index.
🌐
Turing
turing.com › kb › guide-to-numpy-array-slicing
A Useful Guide to NumPy Array Slicing
NumPy is an important library that’s widely used in Python to work with arrays. As seen, to use NumPy, you need to install the library and then import it. It can be used in many ways, such as to slice and index arrays. It can slice either 1-D or 2-D arrays by extracting elements from the original array.
🌐
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]
🌐
Medium
stephencowchau.medium.com › slicing-2d-list-into-columns-in-python-and-corresponding-method-in-numpy-array-and-pytorch-tensor-82f61e656390
Slicing 2D list (inner dimension) in Python and corresponding method in Numpy array and PyTorch tensor | by Stephen Cow Chau | Medium
April 4, 2021 - The Numpy array and PyTorch tensor make it very easy to slice, and with a very similar syntax. In some scenario I might need to work from a list, and here comes one implementation that can be done. A simple 2D number list, I want to slice the input into 3 list like elements
🌐
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 ...
🌐
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
🌐
Python Forum
python-forum.io › thread-36039.html
Slicing a 2 dimensional array
Hi guys, I have the array below: [[1.6727031 1.5464988 1.6836944 1.8492563 1.968533 2.2368639 2.6653275 2.7314425 2.8197284 2.969603 2.8251243 2.086564 2.274447 2.2914152 2.2962196 2.381342 ]]How do I slice it so I have just the last 8 numb...
🌐
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 - #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.
🌐
APXML
apxml.com › courses › essential-numpy-pandas › chapter-3-numpy-array-indexing-slicing › slicing-2d-arrays
Slice 2D NumPy Arrays
# 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.
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.

🌐
Stack Overflow
stackoverflow.com › questions › 71749268 › slice-in-2-dimensional-array
python - Slice in 2 dimensional array - Stack Overflow
2D Python list aren't numpy arrays, they can't handle slicing. Share · Improve this answer · Follow · edited Apr 5, 2022 at 9:43 · answered Apr 5, 2022 at 9:31 · Moun · 32533 silver badges2323 bronze badges · Sign up to request clarification ...
🌐
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.