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
Top answer
1 of 16
6664

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.

๐ŸŒ
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.
Discussions

A Comprehensive Guide to Slicing in Python
The start/end indexing when going in reverse has always taken a level of extra mental effort that I don't like. Like excluding the first and last item using [1:-1] is intuitive to me, but doing the same in reverse by doing [-2:0:-1] annoys me (though I do get why its like that). That is why I tend to do [1:-1][::-1] instead. More on reddit.com
๐ŸŒ r/Python
40
356
February 1, 2022
Does array slicing [::] use extra memory?
Python list slicing should be creating a new list (use more memory). You can see the implementation of that here . I think what you read regarding slicing may be for NumPy arrays where slices create โ€œviewsโ€ and not copies. More on reddit.com
๐ŸŒ r/Python
9
3
December 13, 2021
Numpy Array Slicing
first is select which rows. and then select which elements in each row. 0,2 ...1,2,3 and 7,8,9 1,2 ..... 2 .................9 second is 0,2 so 123,789 1: is a normal slice.. so 2,3 .. 8,9 with this info ..what do you think the arr[1:,2:] will be? ..nice More on reddit.com
๐ŸŒ r/learnpython
9
1
July 9, 2023
Python's slice operator in Kotlin
list[2, 10, 2] seems like an abuse of the set/get operators. But how about: operator fun Iterable.get(range: IntProgression) = asSequence().run { range.mapNotNull { index -> elementAtOrNull(index) } } operator fun MutableList.set(range: ClosedRange, from: Iterable) { for (i in range.start..minOf(range.endInclusive, size - 1)) removeAt(range.start) addAll(range.start, from.toList()) } @Test fun slice() { val list = mutableListOf(5, 6, 7, 8, 9, 10) assertEquals(listOf(7, 8, 9), list[2..4]) assertEquals(listOf(10, 8), list[5 downTo 2 step 2]) list[2..4] = listOf(77) assertEquals(listOf(5, 6, 77, 10), list) list[0..10] = listOf(1, 2, 3) assertEquals(listOf(1, 2, 3), list) } (Behavior for negative indicies is undefined.) More on reddit.com
๐ŸŒ r/Kotlin
6
7
August 29, 2017
๐ŸŒ
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 - By passing 2: in the square brackets, the slicing starts from index 2 (which holds value 3) up until the end of the array, as you can see in the results. For example, if you want to slice an array from the first value to the third, here's how:
๐ŸŒ
Problem Solving with Python
problemsolvingwithpython.com โ€บ 05-NumPy-and-Arrays โ€บ 05.06-Array-Slicing
Array Slicing - Problem Solving with Python
Therefore, the slicing operation [:2] pulls out the first and second values in an array. The slicing operation [1:] pull out the second through the last values in an array. The example below illustrates the default stop value is the last value in the array.
๐ŸŒ
StrataScratch
stratascratch.com โ€บ blog โ€บ numpy-array-slicing-in-python
NumPy Array Slicing in Python - StrataScratch
March 1, 2024 - Negative indices allow you to start counting from the end of the array. # Select the last three elements neg_slice = arr[-3:] print("Last three elements:", neg_slice)
๐ŸŒ
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 ...
๐ŸŒ
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 ...
Find elsewhere
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ array-slicing
NumPy Array Slicing
Here, slice_arr contains elements [2, 3, 4], which are sliced from the original array arr starting at index 2 and ending before index 5.
๐ŸŒ
AskPython
askpython.com โ€บ python โ€บ array โ€บ array-slicing-in-python
Understanding Array Slicing in Python - AskPython
February 12, 2023 - As we can see for both the cases, ... 1. The sliced arrays contain elements of indices 0 to (stop-1). This is one of the quickest methods of array slicing in Python. Again, specifying any two parameters among the start, stop and end, you can perform array slicing in Python by considering default value for the third parameter. Let us take an example...
๐ŸŒ
Nanyang Technological University
libguides.ntu.edu.sg โ€บ python โ€บ arrayslicing
NP.4 Array Slicing - Python for Basic Data Analysis - LibGuides at Nanyang Technological University
July 22, 2025 - 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 ... You can also retrieve elements in a different ...
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python numpy โ€บ numpy array slicing
Numpy Array Slicing - Python Tutorial
August 16, 2022 - First, 1: selects the elements ... to the last elements of the second axis (or column), which returns: ... Use a[m:n:p] to slice one-dimensional arrays....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-slicing-multi-dimensional-arrays
Python Slicing Multi-Dimensional Arrays - GeeksforGeeks
2 weeks ago - 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.
๐ŸŒ
PyTutorial
pytutorial.com โ€บ python-array-slicing-a-complete-guide
PyTutorial | Python Array Slicing: A Complete Guide
March 25, 2026 - You can learn more in our guide on Python Array Append: How to Add Elements. The syntax uses square brackets and a colon. The format is array[start:stop:step]. The start index is inclusive. The slice begins at this position. The stop index is exclusive. The slice ends before this position. The step is the increment. It defaults to 1. All three parameters are optional. Omitting them uses default values. Let's see a basic example.
๐ŸŒ
Analytics Vidhya
analyticsvidhya.com โ€บ home โ€บ python slicing: 9 useful methods for everyday coding
Python Slicing: 9 Useful Methods for Everyday Coding
May 16, 2025 - Byte sequence comes into the picture when we are using the binary data types, and slicing allows you to extract relevant parts of binary data with ease and efficiency. ... byte_seq = b'Hello, world!' # Slice from index 0 to index 5 (exclusive) ...
๐ŸŒ
Turing
turing.com โ€บ kb โ€บ guide-to-numpy-array-slicing
A Useful Guide to NumPy Array Slicing
The minus operator is used to refer to an index from the end of an array; you slice an array from the end instead of from the start. Example: Slice from index 4 (from the end) to index 2 (from the end).
๐ŸŒ
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 ... 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]...
๐ŸŒ
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.
๐ŸŒ
The Python Coding Stack
thepythoncodingstack.com โ€บ the python coding stack โ€บ a slicing story
A Slicing Story - by Stephen Gruppetta
June 25, 2024 - I'll replicate the first example in this Copies and Views subsection, which I'm showing again below, and also use NumPy arrays in addition to using lists: 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.
๐ŸŒ
Python Reference
python-reference.readthedocs.io โ€บ en โ€บ latest โ€บ docs โ€บ brackets โ€บ slicing.html
[] (slicing) โ€” Python Reference (The Right Way) 0.1 documentation
>>> +---+---+---+---+ >>> |-4 |-3 ... >>> +---+---+---+---+ >>> |<- 0:3:1 ->| <= extent of the slice: "ABCD"[0:3:1] ... It can be read as: get every single one item between indexes 0 and 2 (exclusive). The next example shows usage of the step argument:...
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-slice
Python Slice: Useful Methods for Everyday Coding | DataCamp
January 15, 2025 - Versatility across data structures: Slicing works seamlessly across Python's ecosystem, from strings and lists to more advanced structures like NumPy arrays and pandas data frames. Real-world applications: Slicing simplifies everyday coding tasks. Need to process only a segment of a dataset? Extract specific columns from a table? Or reverse a string for a text analysis project? 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.