Python Slicing Explained: Start, Stop and Step With Examples
Python slicing pulls out part of a list, string or tuple with one short expression. Learn how start, stop and step work, why negative indexes count from the end, how to reverse and copy, and how slice assignment changes a list in place, with every output tested.
- Python slicing uses
seq[start:stop:step]. The start is included, the stop is not. - Negative numbers count from the end, so
nums[-3:]gives the last three items. [::-1]reverses a sequence, and[:]makes a shallow copy.- Slices never raise
IndexError. Out-of-range bounds are clipped quietly, which can hide bugs.
Almost every Python program cuts lists and strings into pieces: the first ten rows of a file, the last three log lines, every second item, a reversed name. Python slicing does all of that with one compact expression, and once the rules click you will use it every day.
Every example below was run on Python 3.13.6 and the output is shown exactly as printed. If you are new to the language, our Python tutorial for beginners covers lists and strings first.
What is slicing in Python?
Slicing is a way to read (or replace) a range of items from any sequence: a list, string, tuple, range, bytes or bytearray. Indexing with nums[2] returns one item. Slicing with nums[2:5] returns a new sequence of the same type containing several items. A list slice is a new list, a string slice is a new string and a tuple slice is a new tuple.
Because a slice is a new object, reading a slice never changes the original. Writing to a slice, which we cover later, is the one way slicing modifies a list in place.
Python slicing syntax: start, stop, step
The full form has three parts separated by colons:
sequence[start:stop:step]
- start: the index of the first item to include. Default: the beginning.
- stop: the index where the slice ends. That item is not included. Default: the end.
- step: how far to jump between items. Default: 1. It can be negative, but never 0.
Every part is optional. These are the shapes you will see most:
nums = [10, 20, 30, 40, 50, 60, 70, 80]
print(nums[2:5]) # [30, 40, 50]
print(nums[:3]) # [10, 20, 30]
print(nums[5:]) # [60, 70, 80]
print(nums[::2]) # [10, 30, 50, 70]
print(nums[1::2]) # [20, 40, 60, 80]
A handy check: for positive start and stop, the slice length is stop - start. nums[2:5] has 5 – 2 = 3 items. That half-open design is why nums[:3] and nums[3:] split a list cleanly with no overlap and no gap.
How slice indexes work
The easiest mental model is to picture indexes as the gaps between items, not the items themselves:
+----+----+----+----+----+----+----+----+
| 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 |
+----+----+----+----+----+----+----+----+
0 1 2 3 4 5 6 7 8
-8 -7 -6 -5 -4 -3 -2 -1
A slice [2:5] cuts at gap 2 and gap 5 and keeps whatever sits between them: 30, 40 and 50. Seen this way, [5:] is everything after gap 5, and [:3] is everything before gap 3. The negative row underneath is the subject of the next section.
Negative indexes in slices
A negative index counts from the end: -1 is the last item, -2 the one before. Python converts a negative index by adding the length, so on our 8-item list, -3 becomes 5.
print(nums[-3:]) # [60, 70, 80] last three
print(nums[:-2]) # [10, 20, 30, 40, 50, 60] all but last two
print(nums[-5:-2]) # [40, 50, 60]
This is perfect for log files and queues: lines[-10:] always gives the last ten lines, whatever the file length, and items[:-1] drops a trailing item such as an empty string after a split.
Using a step value
The third number sets the stride. A step of 2 takes every second item, 3 every third, and so on. Combine it with a start to choose the offset:
print(nums[::2]) # [10, 30, 50, 70] even positions
print(nums[1::2]) # [20, 40, 60, 80] odd positions
print(nums[::3]) # [10, 40, 70]
A step of 0 is not allowed and raises ValueError: slice step cannot be zero. Splitting a list into two interleaved halves with [::2] and [1::2] is a common trick for pairing data, often followed by Python zip to walk both halves together.
Reversing with a negative step
With a negative step, the slice walks backwards, so the defaults flip: start defaults to the last item and stop to before the first. That is why [::-1] reverses any sequence:
print(nums[::-1]) # [80, 70, 60, 50, 40, 30, 20, 10]
print(nums[5:1:-1]) # [60, 50, 40, 30]
print(nums[1:5:-1]) # []
s = "stackquora"
print(s[::-1]) # arouqkcats
print("racecar" == "racecar"[::-1]) # True
The third line surprises people. With a negative step, start must be to the right of stop. [1:5:-1] asks Python to walk backwards from 1 to 5, which is impossible, so you get an empty list rather than an error. When a reversed slice comes back empty, swap start and stop.
Out-of-range slices never raise errors
Indexing past the end raises an exception. Slicing past the end does not:
print(nums[2:100]) # [30, 40, 50, 60, 70, 80]
print(nums[100:]) # []
nums[100] # IndexError: list index out of range
Python clips slice bounds to the valid range. This is convenient, since text[:280] is safe for short and long strings alike, but it can also hide a bug. If your code expects exactly five items from row[3:8] and the row is short, you silently get fewer. When the length matters, check it with len() or an assertion.
Tip: in tests, assert the length of important slices. A silent short slice is a classic source of off-by-one bugs that only show up with real data.
Slicing strings and tuples
Strings and tuples slice exactly like lists, and the result keeps the same type:
s = "stackquora"
print(s[:5], s[-5:]) # stack quora
t = (1, 2, 3, 4)
print(t[1:3]) # (2, 3)
print(range(0, 20)[::5]) # range(0, 20, 5)
Note the last line: slicing a range returns another lazy range, not a list, so it costs almost nothing even for huge ranges. Strings are immutable, so you can read slices but never assign to them. To change part of a string, build a new one, for example s[:5] + "-" + s[5:]. For other text jobs, see our guide to Python string methods.
Copying a list with [:]
An empty slice [:] copies the whole list. The copy is a new list object, but it is shallow: nested objects are shared, not duplicated.
grid = [[1, 2], [3, 4]]
copy = grid[:]
copy[0].append(99)
print(grid) # [[1, 2, 99], [3, 4]]
print(copy is grid) # False
The outer list is new, but copy[0] and grid[0] are the same inner list, so appending through the copy changed the original. For nested data you need copy.deepcopy(), explained in our Python deepcopy guide. For flat lists, nums[:], nums.copy() and list(nums) all do the same job.
Slice assignment and deletion
On a mutable list, a slice can appear on the left of =. The selected range is replaced by the items on the right, and the list can grow or shrink:
a = nums[:]
a[1:3] = [99] # replace two items with one
print(a) # [10, 99, 40, 50, 60, 70, 80]
b = nums[:]
b[1:1] = [11, 12] # empty slice = insert
print(b) # [10, 11, 12, 20, 30, 40, 50, 60, 70, 80]
c = nums[:]
del c[::2] # delete every second item
print(c) # [20, 40, 60, 80]
Extended slices (those with a step) are stricter. The replacement must have exactly the same length:
d = nums[:]
d[::2] = [1, 2]
# ValueError: attempt to assign sequence of size 2 to extended slice of size 4
The slice() object
Behind the colon syntax is a built-in slice object. You can create one, name it and reuse it, which makes fixed-width parsing much easier to read:
first_three = slice(0, 3)
print(nums[first_three]) # [10, 20, 30]
print(first_three.indices(len(nums))) # (0, 3, 1)
print(slice(-3, None).indices(8)) # (5, 8, 1)
The indices() method shows the real start, stop and step Python will use for a given length, which is a great way to debug a confusing negative slice. A named slice such as PRICE = slice(20, 28) also documents what each column of a fixed-width record means.
Slicing generators and iterators
Generators are not sequences, so the colon syntax fails:
(x for x in range(5))[1:3]
# TypeError: 'generator' object is not subscriptable
import itertools
squares = (x * x for x in range(10**9))
print(list(itertools.islice(squares, 3, 7))) # [9, 16, 25, 36]
itertools.islice takes start, stop and step like a slice but works lazily, so it read only the first seven values of a billion-item generator. It does not accept negative values. Our Python itertools guide shows more lazy tools.
How fast is slicing?
A slice copies references into a new list, so it costs time and memory in proportion to the slice length. We timed operations on a one-million-item list (best of three runs, 20 calls each):
| Operation | Time per call |
|---|---|
big[::-1] |
8.78 ms |
list(reversed(big)) |
9.25 ms |
big[:] |
8.80 ms |
big.copy() |
8.61 ms |
All four are effectively the same speed, so choose the most readable one. The real cost to watch is slicing inside a loop: for i in range(n): process(data[i:]) copies the tail every time and turns a linear job quadratic. Pass indexes instead, or use islice or enumerate.
Common Python slicing mistakes
- Expecting the stop item.
nums[0:3]returns three items, indexes 0, 1 and 2. The stop is exclusive. - Wrong order with a negative step.
nums[1:5:-1]is empty. With a negative step, start must be larger than stop. - Trusting a slice length. Slices clip silently, so a short input gives a short slice without any error.
- Assuming a deep copy.
[:]copies only the outer list. Nested lists and dicts are shared. - Slicing in a hot loop. Each slice is a new copy; repeated slicing of large lists wastes time and memory.
- Using colon syntax on generators. Use
itertools.isliceinstead.
Python slicing cheat sheet
| Expression | Result |
|---|---|
a[i:j] |
Items from i up to, not including, j |
a[:n] |
First n items |
a[-n:] |
Last n items |
a[:-n] |
All but the last n |
a[::2] |
Every second item |
a[::-1] |
Reversed copy |
a[:] |
Shallow copy |
a[i:i] = x |
Insert x at position i |
del a[i:j] |
Remove a range |
Final thoughts
Python slicing comes down to three numbers and two rules: start is included, stop is not, and negative values count from the end. Add the step for strides and reversal, remember that slices are shallow copies that never raise on bad bounds, and you can handle almost any list or string extraction in one readable line. When slicing is not enough, a list comprehension can filter and transform in the same pass.
Frequently asked questions
What does [::-1] mean in Python?
It is a slice with default start and stop and a step of minus one, so it walks the whole sequence backwards and returns a reversed copy of a list, string or tuple.
Is the stop index included in a Python slice?
No. The start index is included and the stop index is excluded, so nums[2:5] returns the items at indexes 2, 3 and 4.
Why does my slice return an empty list?
Usually the start is past the end of the list, or you used a negative step with start smaller than stop. With a negative step, start must be to the right of stop.
Does slicing copy a list in Python?
Yes, slicing a list returns a new list, but the copy is shallow. Nested lists or dictionaries inside it are shared with the original, so use copy.deepcopy for nested data.
Can you slice a dictionary or a set?
No. Dictionaries and sets are not sequences. Convert the keys or items to a list first, or use itertools.islice to take the first few entries without building a full list.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.