๐ŸŒ
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:
๐ŸŒ
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, ...
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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-list-slicing
Python List Slicing - GeeksforGeeks
Explanation: slice starts at index 1 and stops before index 4, returning the elements at indices 1, 2, and 3.
Published: July 16, 2026
๐ŸŒ
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.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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 ยท
๐ŸŒ
DataCamp
datacamp.com โ€บ home โ€บ tutorials โ€บ python โ€บ python slice: useful methods for everyday coding
Python Slice: Useful Methods for Everyday Coding | DataCamp
January 15, 2025 - Slicing is your go-to. As I said, slicing is a core feature in Python, allowing developers to extract portions of sequences like lists, strings, and tuples. Python offers two primary ways to slice sequences without importing anything: the slicing : syntax and the slice() function.
๐ŸŒ
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.
๐ŸŒ
The Python Coding Stack
thepythoncodingstack.com โ€บ notes from the near future โ€บ a slicing story
A Slicing Story - The Python Coding Stack
September 24, 2023 - Now, let's replicate these steps using NumPy arrays. You'll need to install NumPy using pip install numpy or your favourite package manager: The behaviour when you slice a NumPy array is different to the case when you slice a list. When you assign a new value to an item in subset_of_numbers_array, the original array also changes.
๐ŸŒ
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.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ python slice notation
Python Slice Notation for Strings and Lists | Sentry
September 17, 2026 - Slicing segments an array and returns that segment. In the example below, a string is segmented to return a single word from within the string: hello_world_string = "Hello World!" world = hello_world_string[7:12] print(world) ...
๐ŸŒ
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)
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
๐ŸŒ
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....
๐ŸŒ
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.