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
🌐
freeCodeCamp
freecodecamp.org › news › slicing-and-indexing-in-python
Slicing and Indexing in Python – Explained with Examples
December 11, 2025 - In Python, you perform slicing using the colon : operator. The syntax for slicing is as follows: ... where start_index is the index of the first element in the sub-sequence and end_index is the index of the last element in the sub-sequence ...
🌐
W3Schools
w3schools.com › python › python_strings_slicing.asp
Python - Slicing Strings
Python Examples Python Compiler ... of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string....
🌐
Towards Data Science
towardsdatascience.com › home › latest › mastering indexing and slicing in python
Mastering Indexing and Slicing in Python | Towards Data Science
January 19, 2025 - To perform a slicing over a sequence in Python, you need to provide two offsets separated by a colon although in some cases you can define just one of the two, or even none (more on these cases are discussed below).
🌐
Real Python
realpython.com › lessons › indexing-and-slicing
Indexing and Slicing (Video) – Real Python
In this video, you’ll practice list indexing and slicing. The elements of a list can be accessed by an index. To do that, you name the list, and then inside of a pair of square brackets you use an index number, like what I’m showing right here. That…
Published: September 3, 2019
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-index-and-slice-strings-in-python-3
How To Index and Slice Strings in Python | DigitalOcean
Learn how to index and slice strings in Python 3 with step-by-step examples. Master substring extraction, negative indexing, and slice notation.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-slicing
Python List Slicing - GeeksforGeeks
... Example 2: In this example, ... copy. ... Explanation: slice a[::-1] starts from the end of the list and moves backward one element at a time, returning a reversed copy of the list....
Published: July 16, 2026
🌐
Railsware
railsware.com › home › engineering › indexing and slicing for lists, tuples, strings, other sequential types in python
Python Indexing and Slicing for Lists, Tuples, Strings, other Sequential Types | Railsware Blog
January 22, 2025 - As it was shown, indexing allows you to access/change/delete only a single cell of a list. What if we want to get a sublist of the list. Or we want to update a bunch of cells at once? Or we want to go on a frenzy and extend a list with an arbitrary number of new cells in any position? Those and lots of other cool tricks can be done with slice notation. Let’s look at this subject – feel free to follow along on your own editor or with an online Python compiler.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-index-and-slice-strings-in-python
How to Index and Slice Strings in Python? - GeeksforGeeks
July 15, 2025 - In Python, indexing and slicing are techniques used to access specific characters or parts of a string.
Top answer
1 of 16
6666

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
729

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.

🌐
Medium
medium.com › @shilpasree209 › indexing-slicing-in-python-f45d9caed433
Indexing & Slicing in Python!!!. These 2 things are interesting terms… | by Shilpa Sreekumar | Medium
January 23, 2024 - These 2 things are interesting terms used in Python especially in terms of accessing elements in sequence types like list, string, tuple, range objects etc…Lets look into these terms!!! ... Photo by Henry & Co. on Unsplash · Indexing is a process or technique used to access the elements in a sequence using its position(index) in the sequence.
🌐
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].
🌐
UC Berkeley Statistics
stat.berkeley.edu › ~spector › extension › python › notes › node19.html
Indexing and Slicing
>>> what = 'This parrot is dead' ... piece of a string (known as a slice), use a subscript consisting of the starting position followed by a colon (:, finally followed by one more than the ending position of the slice you want to extract....
🌐
Linode
linode.com › docs › guides › how-to-slice-and-index-strings-in-python
How to Slice and Index Strings in Python | Linode Docs
January 28, 2022 - Python string indexing can also be used to extract a substring from a larger string. This process is referred to as slicing. To use Python to slice a string from a parent string, indicate the range of the slice using a start index and an end index.
🌐
All About AI-ML
indhumathychelliah.com › 2020 › 09 › 22 › indexing-vs-slicing-in-python
Indexing vs Slicing in Python – All About AI-ML
January 2, 2022 - Get to know about indexing and slicing Photo by Bianca Ackermann on Unsplash Indexing and Slicing: In python sequence data types, we can access elements by indexing and slicing. Sequence data types are strings, list, tuple, range objects. Let’s learn about indexing and slicing in detail in ...
Top answer
1 of 3
1

This is a question that keeps coming back over and over. Somehow, this question got relevance in the search engines, but is lacking a comprehensive answer.

The square brackets syntax being discussed here is called "slicing", sometimes also referred as "extended indexing syntax". The formal definition of slicing can be found here, but as any formal definition, it can be very hard to understand for someone just wanting to use it.

In summary what it means, for the most common use-case, is the following:

primary[start:stop:step]

In the notation above:

  • primary: is any variable or literal expression that supports index access, such as a string, a list or a tuple. Other objects may support this syntax too, if implementing the necessary dunder methods (let's keep it simple for now, more about this later).
  • start: the index of the first element that should be included in the slice. Python indices start from 0 (zero)!
  • stop: the index of the element where the slicing should stop. IMPORTANT: the element at the stop position will NOT be included! See examples below.
  • step: (also called stride) represents the size of the "jump" of each iteration from start to stop

Some additional notes:

  • A negative integer for start or stop means counting from the end of the list backwards, but the direction of the slice is still the same (which is controlled by the sign of step). That is to say, negative numbers are just an alternative way to place the starting and ending point of a slice
  • A negative integer for the step inverts the direction and the sorting of the resulting slice, effectively starting from the stop (this time inclusive) position and going backwards to the start (exclusive) position.

Just to be clear:

  • If step is not set or positive, the slice will include the element at start index, but not the one at stop index;
  • If step is negative, the slice will include the element at stop index, but not the one at start index. Also, the resulting list will have the elements in reverse order.

I know all this sounds confusing and the examples below should help to clarify:

>>> numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> numbers[2:9]          # slice from 3rd to 9th items (9th not included)
[2, 3, 4, 5, 6, 7, 8]
>>> numbers[-8:-1]        # Index -8 == index 2 and index -1 == index 9
[2, 3, 4, 5, 6, 7, 8]
>>> numbers[2:9:2]        # Take every second element in the slice
[2, 4, 6, 8]
>>> numbers[-8:-1:2]      # Same effect as above
[2, 4, 6, 8]

>>> numbers[2:9:-1]       # Trying to go from 3rd to 9th element in reverse
[]                        # results in an empty list

>>> numbers[9:2:-1]       # Step is now negative. Slice is in reverse order,
[9, 8, 7, 6, 5, 4, 3]     # from `stop` to `start` position (not included)

>>> numbers[-1:-8:-1]     # Same effect as above
[9, 8, 7, 6, 5, 4, 3]
>>> numbers[-1:-8:-2]     # Take every second element in reverse
[9, 7, 5, 3]

Some useful shorthand forms:

>>> numbers[:]                   # shallow copy
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> numbers[::-1]                # reversed shallow copy
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

As mentioned above, third party objects can support this syntax if implementing the __getitem__ dunder method for integer keys. Remarkable examples are NumPy, Pandas, and many others.

Notes about the original question:

The OP reports dealing with a list x of 32 elements and wants to do some math with it. More specifically they say:

  • x[:-N+1] I want to access all elements except the last two
  • x[N:-N] I want to access all elements except the first one and the last one
  • x[N+1:] I want to access all elements except the first

I'm not entirely sure what they meant by N in the formulas, but the way to achieve the desired result is rather simple:

  • To access all elements except the last two: x[:-2]
  • To access all elements except the first and last one: x[1:-1]
  • To access all elements except the first: x[1:]

Notice, however, that the first two slices will have 30 elements and the third slice will have 31. Doing algebraic operations with matrices/vectors of different shape won't work. That's probably something you'll want to fix.

2 of 3
0

Slice except the last two: x[:N-2] Except first and last: x[1:N-1] Except first two: x[2:]

Python slice can be obtained by: x[starting_index:end_index] {including starting_index element and excluding end_index}

If you don't specify the starting_index, it becomes 0 by default. If you don't specify the end_index, it becomes N by default.

🌐
Python.org
discuss.python.org › python help
What is the point of the slice.indices method? - Python Help - Discussions on Python.org
July 22, 2023 - What is the point of the slice.indices method, since we have the following equality? s = slice(start, stop, step) assert range(*s.indices(length)) == range(length)[s]
🌐
Python Morsels
pythonmorsels.com › slicing
Python list slicing (with examples) - Python Morsels
March 8, 2024 - What do you think we might get ... 'pear', 'lemon', 'orange'] With Python's slicing syntax, the first item is the start index, and the second item is the stop index....
🌐
DevGenius
blog.devgenius.io › indexing-vs-slicing-in-python-de01cd99c499
Indexing vs Slicing in Python. Get to know about indexing and slicing | by Indhumathy Chelliah | Dev Genius
January 25, 2021 - Indexing vs Slicing in Python Get to know about indexing and slicing Indexing and Slicing: In python sequence data types, we can access elements by indexing and slicing. Sequence data types are …
🌐
DataCamp
datacamp.com › tutorial › python-slice
Python Slice: Useful Methods for Everyday Coding | DataCamp
January 15, 2025 - Understanding both methods is useful ... of a sequence. Each parameter—start, stop, and step—controls how the slicing is performed: start: Index where the slice begins (inclusive)....
🌐
NumPy
numpy.org › doc › stable › user › basics.indexing.html
Indexing on ndarrays — NumPy v2.5 Manual
So one can use code to construct tuples of any number of indices and then use these within an index. Slices can be specified within programs by using the slice() function in Python.