๐ŸŒ
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, ...
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ array โ€บ array-slicing-in-python
Understanding Array Slicing in Python - AskPython
March 30, 2020 - Array slicing can be easily done following the Python slicing method. For which the syntax is given below. ... Again, Python also provides a function named slice() which returns a slice object containing the indices to be sliced.
Discussions

slice - How slicing in Python works - Stack Overflow
How does Python's slice notation work? That is: when I write code like a[x:y:z], a[:], a[::2] etc., how can I understand which elements end up in the slice? See Why are slice and range upper-bound More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python: slicing a multi-dimensional array - Stack Overflow
@mgilson I get it now, under normal circumstances a[:] is a copy and is used to avoid having multiple pointers referencing the same memory location. I just confused C and python syntax. 2016-05-11T09:24:48.53Z+00:00 ... To slice a multi-dimensional array, the dimension (i.e. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python: Slicing lists

Nevermind I understand it now

More on reddit.com
๐ŸŒ r/learnprogramming
5
3
May 30, 2019
Help w slicing array distinctions

This is because the two expressions mean different things. You can think about a two-dimensional array as an array of arrays, something like this:

[
[_, _, _],
[_, _, _],
[_, _, _],
]

When you use the subscript you use, (btw. 0 is not necessary there and might be a bit confusing: a[0:, 2] == a[:, 2]), you say this:

Select all rows and then select the third element

and this is what gets selected:

[
[_, _, _],
[_, _, _],
[x, x, x],
]

However, when you do a[:, 2:], you are saying:

Select all rows, and then select the last column

And this is selected:

[
[_, _, x],
[_, _, x],
[_, _, x],
]

You would've been right to assume that these two should do the same, had it not being for the "direction" of rows.

More on reddit.com
๐ŸŒ r/learnpython
12
1
December 30, 2019
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-slicing-how-to-slice-an-array
Python Slicing โ€“ How to Slice an Array and What Does [::-1] Mean?
December 8, 2022 - It's also worth noting that the slicing action does not affect the original array. With slicing, you only "copy a portion" of the original array. For example, if you want to slice an array from a specific start value to the end of the array, here's how:
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 509211 โ€บ how-slicing-in-python-works
slice - How slicing in Python works - Stack Overflow
How does Python's slice notation work? That is: when I write code like a[x:y:z], a[:], a[::2] etc., how can I understand which elements end up in the slice? See Why are slice and range upper-bound
๐ŸŒ
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.
๐ŸŒ
Pythonhealthdatascience
pythonhealthdatascience.com โ€บ content โ€บ 01_algorithms โ€บ 03_numpy โ€บ 03_slicing.html
Array slicing and indexing โ€” Python for health data science.
Going back to slicing we can understand the dimensions. Hereโ€™s a reminder fo the full array: ... Task: slice td so that we have the vector [13, 23, 33] i.e. the [i, 1, 0] elements where i is the row.
๐ŸŒ
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 ...
๐ŸŒ
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. Python ยท import numpy as np matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) matrix[0:2, 0:2] = 0 print(matrix) Output ยท
Find elsewhere
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ array-slicing
NumPy Array Slicing
Array slicing is utilized when ... 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....
๐ŸŒ
StrataScratch
stratascratch.com โ€บ blog โ€บ numpy-array-slicing-in-python
NumPy Array Slicing in Python - StrataScratch
March 1, 2024 - The basic syntax for array slicing in NumPy follows the format: array[start:stop:step]. Here's what each part means: start: The index at which the slice starts.
๐ŸŒ
Python Central
pythoncentral.io โ€บ how-to-slice-listsarrays-and-tuples-in-python
How to Slice Lists/Arrays and Tuples in Python | Python Central
July 18, 2022 - On the other hand, entering the stop parameter is mandatory since it indicates to Python the index to which slicing is supposed to take place. By default, Python will stop the slicing at the value โ€œstop - 1.โ€ ยท In other words, if the stop value is 5 when using the slice function, Python will only return values till the index value is 4.
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ data science โ€บ slicing in python: a comprehensive guide
Slicing in Python: A Comprehensive Guide | Towards Data Science
June 5, 2024 - In this article, we have explored slicing techniques available in Python for various data structures, from basic lists and tuples to more complex NumPy arrays and Pandas dataframes. We've seen how powerful and versatile slicing can be, allowing for efficient and intuitive data manipulation. We focused specifically on slicing, which involves selecting continuous subsets of elements using the start:stop:step syntax or slice objects.
๐ŸŒ
DataCamp
datacamp.com โ€บ home โ€บ tutorials โ€บ python โ€บ python slice: useful methods for everyday coding
Python Slice: Useful Methods for Everyday Coding | DataCamp
January 15, 2025 - Understanding whether Python creates ... creates a copy of the original sequence, but with NumPy arrays, slicing creates a view, meaning both the original array and the slice point to the same data in memory....
๐ŸŒ
CodeRivers
coderivers.org โ€บ blog โ€บ python-array-slicing
Python Array Slicing: A Comprehensive Guide - CodeRivers
February 22, 2026 - The basic syntax for slicing an array in Python is sequence[start:stop]. Here, start is the index of the first element you want to include in the slice, and stop is the index of the first element you want to exclude from the slice.
๐ŸŒ
PyTutorial
pytutorial.com โ€บ python-array-slicing-a-complete-guide
PyTutorial | Python Array Slicing: A Complete Guide
March 25, 2026 - Remember, slicing creates a new object. Modifying the slice does not affect the original. This is key for a solid Python Array Implementation Guide.
๐ŸŒ
Codebilby
codebilby.com โ€บ blog โ€บ a50-array-slicing-in-python
Array slicing in Python - Code Bilby
February 8, 2025 - # Slice from index 1 to 8 with a step of 2 slice7 = arr[1:8:2] print(slice7) # Output: [1, 3, 5, 7] # Slice the entire list with a step of 3 slice8 = arr[::3] print(slice8) # Output: [0, 3, 6, 9] # Reverse the list using slicing slice9 = arr[::-1] print(slice9) # Output: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
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.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ array-slicing
NumPy Array Slicing (With Examples)
May 22, 2023 - 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.
๐ŸŒ
Python Basics
pythonbasics.org โ€บ home โ€บ python basics โ€บ how to slice lists/arrays in python
How to Slice Lists/Arrays in Python - pythonbasics.org
In this case we took a slice from the list pizza, the output is stored into a new variable. If you want you can pass it directly to the print function. Create a list of persons. We'll use the slicing technique to get the first two persons in the list. ... #!/usr/bin/python persons = [ "John", "Marissa", "Pete", "Dayton" ] slice = persons[0:2] print(slice)