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.

🌐
StrataScratch
stratascratch.com › blog › numpy-array-slicing-in-python
NumPy Array Slicing in Python
March 1, 2024 - Then, we slice this array from index 1 to index 5 (inclusive of 1 and exclusive of 6), resulting in a new array sliced_arr that contains the elements [1 2 3 4 5].
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-slicing
Python List Slicing - GeeksforGeeks
In Python, list slicing allows out-of-bound indexing without raising errors. If we specify indices beyond the list length then it will simply return the available items. Example: The slice a[7:15] starts at index 7 and attempts to reach index 15, but since the list ends at index 8, so it will return only the available elements (i.e.
Published   July 23, 2025
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › regarding list slicing: can anyone help me understand the reasoning behind inclusive vs. exclusive indexing with negative vs. non-negative integers?
r/learnpython on Reddit: Regarding list slicing: can anyone help me understand the reasoning behind inclusive vs. exclusive indexing with negative vs. non-negative integers?
April 27, 2019 -

Python beginner here, coming from a Matlab and R background... so there's been a bit of a hurdle for me when it comes to understanding indexing in Python.

My current brain fuck has to do with wrapping my head around indexing and list ranges/slicing. And no amount of Google-fu is helping me here.

Let's say I have a simple list:

list = ["a", "b", "c", "d"]

If I want to take a slice of the list, the end of the range is exclusive when using non-negative values:

print(list[0:1])
['a']

Not super intuitive given my background, but fine. I understand what's going on and generally why it happens.

What I don't understand is why the end of the range is inclusive when using negative indexing:

print(list[0:-1])
['a', 'b', 'c']

It seems that the slice should only include ['a', 'b']: given how Python indexes a range of non-negative values, I would expect the result to only give the list items up to (but not including) the penultimate item in the list.

What's the rationale behind an exclusive range for non-negative values but an inclusive range for negative values?

🌐
DataCamp
datacamp.com › tutorial › python-slice
Python Slice: Useful Methods for Everyday Coding | DataCamp
January 15, 2025 - If you're tired of fumbling with confusing slice indices or wondering why your sliced lists aren't behaving as expected, you’re in the right place. In this guide, we’ll break down Python slicing into digestible chunks (pun intended). We’ll cover everything from the basics of slicing syntax to advanced techniques with multi-dimensional arrays.
🌐
Wikipedia
en.wikipedia.org › wiki › Array_slicing
Array slicing - Wikipedia
3 weeks ago - With Python standard lists (which are dynamic arrays), every slice is a copy. Slices of NumPy arrays, by contrast, are views onto the same underlying buffer. ... Both bounds are inclusive and can be omitted, in which case they default to the declared array bounds.
🌐
freeCodeCamp
freecodecamp.org › news › slicing-and-indexing-in-python
Slicing and Indexing in Python – Explained with Examples
December 11, 2025 - In these examples, we have used slicing to extract a range of characters from the sentence string. The 4:9 slice means that we are selecting characters starting from index 4 (inclusive) up to index 9 (exclusive), which correspond to the second ...
🌐
W3Schools
w3schools.com › python › python_strings_slicing.asp
Python - Slicing Strings
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... You can return a range of characters by using the slice syntax....
🌐
Data Dependence
datadependence.com › 2016 › 05 › python-sequence-slicing-guide
A Quick Guide to Slicing in Python – Become a Python Ninja
July 6, 2016 - For example if you choose the starting ... index 5 will not be included. One way to remember this is that the start point is inclusive and the end point is exclusive....
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › python faq
Why and how are selecting python list indexes inclusive and exclusive? - Python FAQ - Codecademy Forums
July 1, 2019 - Answer When selecting from a list in python, you may have noticed the saying ‘inclusive’ or ‘exclusive’, this is usually referring to the slice notation mylist[start:stop] where the start index is inclusive but the stop index is exclusive.
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
What is slicing in Python? - Python - Data Science Dojo Discussions
November 9, 2022 - Slicing enables the extraction of a designated range of elements from arrays, lists, strings, or tuples using the syntax [start : stop : step]. Here, start denotes the beginning or starting index, stop indicates the ending index, and step determines ...
🌐
O'Reilly
oreilly.com › library › view › pandas-for-everyone › 9780134547046 › app12.xhtml
L. Slicing Values - Pandas for Everyone: Python Data Analysis, First Edition [Book]
December 15, 2017 - L. Slicing Values Python is a zero-indexed language (things start counting from zero), and is also left inclusive, right exclusive you are when specifying a range of values. This...
Author   Daniel Y. Chen
Published   2017
Pages   410
🌐
Programiz
programiz.com › python-programming › numpy › array-slicing
NumPy Array Slicing (With Examples)
... Array Slicing is the process of extracting a portion of an array. With slicing, we can easily access elements in the array. It can be done on one or more dimensions of a NumPy array. ... Note: When we slice arrays, the start index is inclusive but the stop index is exclusive.
🌐
w3resource
w3resource.com › python-exercises › pandas_numpy › pandas_numpy-exercise-23.php
Slicing and extracting a portion of a NumPy array
We create a NumPy array "nums". The slice notation nums[2:6] is used to extract elements from index 2 to 5 (inclusive). Note that the ending index is exclusive in Python slicing.