Python Tuple Slicing Explained
Tuple slicing lets you get part of a tuple by using square brackets and colons. It is a simple way to take a range of values, skip values with a step, or reverse a tuple.
Use this format:
tuple[start:stop:step]
The stop position is not included.
Quick example #
numbers = (10, 20, 30, 40, 50)
print(numbers[1:4]) # (20, 30, 40)
print(numbers[:3]) # (10, 20, 30)
print(numbers[::2]) # (10, 30, 50)
print(numbers[::-1]) # (50, 40, 30, 20, 10)
numbers[1:4] keeps indices 1, 2, 3 — stop index 4 is not included.What tuple slicing means #
Slicing gets part of a tuple.
- It uses square brackets with colons
- The result is a new tuple
- It does not change the original tuple
Example:
letters = ("a", "b", "c", "d", "e")
part = letters[1:4]
print(part)
print(letters)
Output:
('b', 'c', 'd')
('a', 'b', 'c', 'd', 'e')
The original tuple stays the same because tuples are immutable. If you are new to tuples, see Python tuples explained.
Basic slice syntax #
The full syntax is:
my_tuple[start:stop:step]
Each part has a job:
start: where slicing beginsstop: where slicing ends, but that index is not includedstep: how many positions to move each time
All three parts are optional.
Example:
numbers = (10, 20, 30, 40, 50, 60)
print(numbers[1:5:2])
Output:
(20, 40)
This starts at index 1, stops before index 5, and moves by 2.
If you need a refresher on getting a single value, read Python tuple indexing explained.
Common slicing patterns #
These are the most common tuple slice patterns you will use.
Get the first n items #
numbers = (10, 20, 30, 40, 50)
print(numbers[:3])
Output:
(10, 20, 30)
Get items from an index to the end #
numbers = (10, 20, 30, 40, 50)
print(numbers[2:])
Output:
(30, 40, 50)
Get a range of items #
numbers = (10, 20, 30, 40, 50)
print(numbers[1:4])
Output:
(20, 30, 40)
Get every second item #
numbers = (10, 20, 30, 40, 50, 60)
print(numbers[::2])
Output:
(10, 30, 50)
Reverse a tuple #
numbers = (10, 20, 30, 40, 50)
print(numbers[::-1])
Output:
(50, 40, 30, 20, 10)
Negative indexes in slices #
Negative indexes count from the end of the tuple.
This is useful when you want values near the end without calculating the tuple length first.
Get the last 3 items #
numbers = (10, 20, 30, 40, 50, 60)
print(numbers[-3:])
Output:
(40, 50, 60)
Get everything except the last item #
numbers = (10, 20, 30, 40, 50)
print(numbers[:-1])
Output:
(10, 20, 30, 40)
Negative values are especially helpful when the tuple size may change. If you want to check the size first, see Python tuple length with len().
Step values and reversing #
The step value controls how slicing moves.
- A positive step moves left to right
- A negative step moves right to left
tuple[::-1]is a common way to reverse a tuple- A step of
0is not allowed
Example with a positive step:
numbers = (10, 20, 30, 40, 50, 60)
print(numbers[1:6:2])
Output:
(20, 40, 60)
Example with a negative step:
numbers = (10, 20, 30, 40, 50)
print(numbers[4:1:-1])
Output:
(50, 40, 30)
Be careful with this invalid slice:
numbers = (10, 20, 30)
print(numbers[::0])
This raises an error because the step cannot be zero.
What slicing returns #
A tuple slice returns a tuple, not a list.
A normal slice returns a tuple #
numbers = (10, 20, 30, 40)
result = numbers[1:3]
print(result)
print(type(result))
Output:
(20, 30)
<class 'tuple'>
A one-item slice is still a tuple #
numbers = (10, 20, 30)
result = numbers[1:2]
print(result)
print(type(result))
Output:
(20,)
<class 'tuple'>
An empty slice returns an empty tuple #
numbers = (10, 20, 30)
print(numbers[5:8])
Output:
()
If you need help creating tuples correctly, including one-item tuples, see Python tuple: creating a tuple.
Tuple slicing vs tuple indexing #
Slicing and indexing are related, but they are not the same.
- Indexing gets one item
- Slicing gets multiple items or a range
- Indexing can raise
IndexError - Slicing usually returns an empty tuple if the range does not match
Example:
numbers = (10, 20, 30)
print(numbers[1]) # one item
print(numbers[1:5]) # slice
print(numbers[5:8]) # empty slice
Output:
20
(20, 30)
()
Now compare that with invalid indexing:
numbers = (10, 20, 30)
print(numbers[5])
This raises an IndexError. If you run into that problem, read IndexError: tuple index out of range.
Beginner mistakes to avoid #
These are the most common tuple slicing mistakes:
- Thinking the stop index is included
- Using parentheses instead of square brackets
- Expecting slicing to change the original tuple
- Forgetting that step cannot be
0
Here are a few examples.
Mistake: thinking the stop index is included #
numbers = (10, 20, 30, 40, 50)
print(numbers[1:4])
Output:
(20, 30, 40)
Index 4 is not included.
Mistake: using parentheses #
This is wrong:
numbers = (10, 20, 30, 40)
# print(numbers(1:3))
Use square brackets instead:
numbers = (10, 20, 30, 40)
print(numbers[1:3])
Mistake: expecting the original tuple to change #
numbers = (10, 20, 30, 40)
slice_result = numbers[:2]
print(slice_result)
print(numbers)
Output:
(10, 20)
(10, 20, 30, 40)
The original tuple is unchanged.
Common causes of confusion #
Many tuple slicing problems come from a few simple misunderstandings:
- Confusing slicing with indexing
- Expecting the stop index to be included
- Using a zero step like
tuple[::0] - Assuming tuple slices return a list
- Using parentheses instead of brackets
If your slice is not giving the result you expect, print the tuple, the slice, and its type.
print(my_tuple)
print(my_tuple[1:4])
print(type(my_tuple[1:4]))
print(len(my_tuple))
These checks help you confirm:
- what the tuple contains
- what the slice actually returns
- that the result is a tuple
- how many items are in the original tuple
FAQ #
Does tuple slicing change the original tuple? #
No. It returns a new tuple and leaves the original tuple unchanged.
Is the stop index included in a tuple slice? #
No. The stop index is excluded.
What does tuple[::-1] do? #
It returns a reversed copy of the tuple.
Can tuple slicing cause an IndexError? #
Normal slicing usually does not. Invalid indexing is more likely to cause IndexError.