The syntax is:

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

There is also the step value, which can be used with any of the above:

a[start:stop:step] # start through not past stop, by step

The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default).

The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning. So:

a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

Similarly, step may be a negative number:

a[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

Relationship with the slice object

A slice object can represent a slicing operation, i.e.:

a[start:stop:step]

is equivalent to:

a[slice(start, stop, step)]

Slice objects also behave slightly differently depending on the number of arguments, similar to range(), i.e. both slice(stop) and slice(start, stop[, step]) are supported. To skip specifying a given argument, one might use None, so that e.g. a[start:] is equivalent to a[slice(start, None)] or a[::-1] is equivalent to a[slice(None, None, -1)].

While the :-based notation is very helpful for simple slicing, the explicit use of slice() objects simplifies the programmatic generation of slicing.

Answer from Greg Hewgill on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_slicing.asp
NumPy Array Slicing
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, but excludes the end index.
Top answer
1 of 16
6657

The syntax is:

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array

There is also the step value, which can be used with any of the above:

a[start:stop:step] # start through not past stop, by step

The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default).

The other feature is that start or stop may be a negative number, which means it counts from the end of the array instead of the beginning. So:

a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items

Similarly, step may be a negative number:

a[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed

Python is kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen.

Relationship with the slice object

A slice object can represent a slicing operation, i.e.:

a[start:stop:step]

is equivalent to:

a[slice(start, stop, step)]

Slice objects also behave slightly differently depending on the number of arguments, similar to range(), i.e. both slice(stop) and slice(start, stop[, step]) are supported. To skip specifying a given argument, one might use None, so that e.g. a[start:] is equivalent to a[slice(start, None)] or a[::-1] is equivalent to a[slice(None, None, -1)].

While the :-based notation is very helpful for simple slicing, the explicit use of slice() objects simplifies the programmatic generation of slicing.

2 of 16
728

The Python tutorial talks about it (scroll down a bit until you get to the part about slicing).

The ASCII art diagram is helpful too for remembering how slices work:

 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
   0   1   2   3   4   5
  -6  -5  -4  -3  -2  -1

One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n.

๐ŸŒ
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 - We also accessed the second value by using square brackets and its index in the order, which is 1. Let's say you want to slice a portion of this array and assign the slice to another variable. You can do it using colons and square brackets. The ...
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ array-slicing
NumPy Array Slicing
import numpy as np # Syntax array_slice = array[start:stop:step]
๐ŸŒ
StrataScratch
stratascratch.com โ€บ blog โ€บ numpy-array-slicing-in-python
NumPy Array Slicing in Python
March 1, 2024 - The slicing operation returns a ... handling to avoid unintended changes. The basic syntax for array slicing in NumPy follows the format: array[start:stop:step]. Here's what each part means:...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ array-slicing
NumPy Array Slicing (With Examples)
Array Slicing is the process of ... dimensions of a NumPy array. Syntax of NumPy Array Slicing Here's the syntax of array slicing in NumPy: array[start:stop:step] Here,...
๐ŸŒ
Nanyang Technological University
libguides.ntu.edu.sg โ€บ python โ€บ arrayslicing
NP.4 Array Slicing - Python for Basic Data Analysis - LibGuides at Nanyang Technological University
Let's create a simple array with shape (3,3) to work with. With a (3,3) shaped array, we know that its rows and columns can be referred to using their indices: 0, 1 and 2. ... To retrieve all the elements from the first row, specify the row parameter as 0, and leave the column parameter empty. Syntax
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ python slice notation
Python slice notation | Sentry
In the example below, a string is segmented to return a single word from within the string: ... The start is the index of the array that you would like the slice to begin with, the stop is the index of the array you would like to stop at.
Find elsewhere
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.4 Manual
The standard rules of sequence slicing apply to basic slicing on a per-dimension basis (including using a step index). Some useful concepts to remember include: The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step (\(k\neq0\)).
๐ŸŒ
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ a comprehensive guide to slicing in python
r/Python on Reddit: A Comprehensive Guide to Slicing in Python
February 1, 2022 - I've always found array slicing in python to be very powerful and useful, and a thing i wish more languages had. For my common use case (dealing with text) it's very fast to code something up. ... I'm very new to Python and one thing I find confusing and unintuitive is that some things, like slices, start counting at 0 but other things like groups in regular expressions start counting at 1.
๐ŸŒ
Problem Solving with Python
problemsolvingwithpython.com โ€บ 05-NumPy-and-Arrays โ€บ 05.06-Array-Slicing
Array Slicing - Problem Solving with Python
Where <slice> is the slice or section of the array object <array>. The index of the slice is specified in [start:stop]. Remember Python counting starts at 0 and ends at n-1. The index [0:2] pulls the first two values out of an array.
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ understanding array slicing in python
Understanding Array Slicing in Python - AskPython
February 12, 2023 - In this case, too, the sliced array module array and NumPy array contain elements of indices specified start to (stop-1) with step set to 1. The output is hence justified. When all the three parameters are mentioned, you can perform array slicing in Python from index start to (stop-1) with each index jump equals to the given step.
๐ŸŒ
The Python Coding Stack
thepythoncodingstack.com โ€บ p โ€บ a-python-slicing-story
A Slicing Story - by Stephen Gruppetta
September 24, 2023 - You can read more about __getitem__() ... does since list is the superclass for TestList. The syntax 2:7 within the square brackets represents a slice object....
๐ŸŒ
Pythonhealthdatascience
pythonhealthdatascience.com โ€บ content โ€บ 01_algorithms โ€บ 03_numpy โ€บ 03_slicing.html
Array slicing and indexing โ€” Python for health data science.
Originally we were restricting our slice to the 1st element i.e. k=0 of each array returned. If we want all elements we replace the 0 with : ... Getting to grips with multi-dimensional arrays takes time and practice. So do persevere. If you have only ever coded in python before and have no experience of a lower level language like C++ or Rust then the behaviour of arrays and slices can catch you out.
๐ŸŒ
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 - Getting the first โ€œNโ€ elements from a list with slice is as simple as using the following syntax: ... Letโ€™s take the same list as the example before, and output the first three elements of the list by slicing: [python] items = ['car', 'bike', 'house', 'bank', 'purse', 'photo', 'box'] sub_items = items[:3] print(sub_items) [/python]
๐ŸŒ
Python
docs.python.org โ€บ 2.3 โ€บ whatsnew โ€บ section-slices.html
https://docs.python.org/2.3/whatsnew/section-slice...
Ever since Python 1.4, the slicing syntax has supported an optional third ``step'' or ``stride'' argument. For example, these are all legal Python syntax: L[1:10:2], L[:-1:1], L[::-1]. This was added to Python at the request of the developers of Numerical Python, which uses the third argument ...
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-slice
Python Slice: Useful Methods for Everyday Coding | DataCamp
January 15, 2025 - Understanding whether Python creates a copy or a view is essential for memory management when slicing data structures, especially with large datasets. Know that with built-in lists and strings, Slicing always 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.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.2 Manual
The standard rules of sequence slicing apply to basic slicing on a per-dimension basis (including using a step index). Some useful concepts to remember include: The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step (\(k\neq0\)).