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
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
๐ŸŒ
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.
๐ŸŒ
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, ...
๐ŸŒ
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]
๐ŸŒ
StrataScratch
stratascratch.com โ€บ blog โ€บ numpy-array-slicing-in-python
NumPy Array Slicing in Python
March 1, 2024 - import numpy as np # Create a ... from index 3 to 7 with step 2:", slice3) Here is the output. Slicing 2D arrays in NumPy allows you to access subsets of the array's rows and columns....
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ array-slicing
NumPy Array Slicing (With Examples)
The only difference is that we need to specify a slice for each dimension of the array. array[row_start:row_stop:row_step, col_start:col_stop:col_step] ... row_start,row_stop,row_step - specifies starting index, stopping index, and step size for the rows respectively ยท col_start,col_stop,col_step ...
๐ŸŒ
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.
๐ŸŒ
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 ...
Find elsewhere
๐ŸŒ
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/learnpython โ€บ problems with slicing 2d array
r/learnpython on Reddit: Problems with slicing 2D array
October 26, 2022 -

Hi, I'm trying to create a simple game of life and I have problems putting zeros on the edge of my grid. When i try the following code i get an error and I don't understand why.

the variable grid represent a 2D array.

the code:

grid[0,:] = np.zeros_like(grid[0,:])#top_row

grid[-1,:] = np.zeros_like(grid[-1,:])#bottom_row

grid[:,0] = np.zeros_like(grid[:,0])#left

grid[:,-1] = np.zeros_like(grid[:,-1])#right

the error:

TypeError: list indices must be integers or slices, not tuple

๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
Problem Solving with Python
problemsolvingwithpython.com โ€บ 05-NumPy-and-Arrays โ€บ 05.06-Array-Slicing
Array Slicing - Problem Solving with Python
The code section below creates a two row by four column array and indexes out the first two rows and the first three columns. ... The code section below slices out the first two rows and all columns from array a.
๐ŸŒ
Schnell-web
ilan.schnell-web.net โ€บ prog โ€บ slicing
Ilan Schnell - Multi dimensional slicing in Python
However, Python does not come with multi axis arrays, it only supports the syntax. To understand better in which way Pythons supports multi axis slicing syntax, the following tiny class to exposes the arguments passed to the __getitem__ method. >>> class Foo: ... def __getitem__(self, *args): ... print args ... >>> x = Foo() >>> x[1] (1,) >>> x[1:] (slice(1, 2147483647, None),) >>> x[1:, :] ((slice(1, None, None), slice(None, None, None)),) >>> x[1:, 20:10:-2, ...] ((slice(1, None, None), slice(20, 10, -2), Ellipsis),)
๐ŸŒ
Pythoninformer
pythoninformer.com โ€บ python-libraries โ€บ numpy โ€บ index-and-slice
PythonInformer - Indexing and slicing numpy arrays
You can also use a slice of length 1 to do something similar (slice 1:2 instead of index 1): ... Notice the subtle difference. The first creates a 1D array, the second creates a 2D array with only one row. Sign up using this form to receive an email when new content is added to the graphpicmaths or pythoninformer websites:
๐ŸŒ
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
๐ŸŒ
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...
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
Nanyang Technological University
libguides.ntu.edu.sg โ€บ python โ€บ arrayslicing
NP.4 Array Slicing - Python for Basic Data Analysis - LibGuides at Nanyang Technological University
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 ...