Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Tuple Slicing: Syntax, Steps, and Pitfalls

Learn how to slice tuples in Python, including start, stop, and step behavior, negative indexes, and common edge cases.

~4 min readPython
#tuple slicing#python sequences#immutability#step parameter
Diagram showing a tuple slice with start, stop, and step parameters producing a new tuple

Python tuple slicing uses the same bracket syntax as lists. Slicing a tuple returns a tuple containing the selected elements, not a view into the original tuple. Because tuples are immutable, you cannot modify the original tuple through a slice; extracting a subset is normally implemented by copying the selected elements into a new tuple.

How Tuple Slicing Works

A tuple slice is written as tuple[start:stop:step]. The start index is inclusive, stop is exclusive, and step defaults to 1. With a positive step, an omitted start means the beginning of the tuple, and an omitted stop means the end.

python
t = (10, 20, 30, 40, 50) print(t[1:4]) # (20, 30, 40) print(t[:3]) # (10, 20, 30) print(t[2:]) # (30, 40, 50)

The result is a tuple. For a partial slice, this means the selected elements are copied into a new tuple object. For an exhaustive slice such as t[:], do not rely on whether Python returns the same object or a new object; implementations may reuse the immutable tuple. Unlike list slicing, tuple slicing is a selection operation, not a way to modify part of the original sequence.

Start, Stop, and Step Semantics

The slice interval is half-open: start is included, stop is excluded. This matches Python's range behavior and avoids off-by-one errors when chaining slices. The step parameter controls which elements are selected within that interval.

python
t = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) print(t[2:8:2]) # (2, 4, 6) print(t[::3]) # (0, 3, 6, 9)

When step is positive, start defaults to 0 and stop defaults to len(t). When step is negative, the defaults swap: start defaults to len(t) - 1, and stop defaults to a sentinel before the beginning of the tuple. Because stop is still exclusive, that sentinel means t[::-1] includes index 0.

Negative Indexes and Negative Steps

Negative indexes count from the end of the tuple. -1 refers to the last element, -2 to the second last, and so on. This works in slices exactly as it does for single-element indexing.

python
t = (10, 20, 30, 40, 50) print(t[-3:]) # (30, 40, 50) print(t[:-2]) # (10, 20, 30) print(t[::-1]) # (50, 40, 30, 20, 10)

The [::-1] idiom is the standard way to reverse a tuple. It uses a negative step to traverse from the end to the beginning. Note that a negative step with default start and stop includes every element, but the order is reversed.

When you combine negative indexes with a negative step, the semantics can be confusing. For example, t[-2:-5:-1] extracts elements from index -2 down to index -5 (exclusive), which means indices -2, -3, -4.

python
t = (10, 20, 30, 40, 50) print(t[-2:-5:-1]) # (40, 30, 20)

Slicing Returns a Tuple, Not a View

Tuple slicing returns a tuple containing the selected elements. For a partial slice, this normally means a new tuple is allocated and the selected elements are copied. What is guaranteed is that the original tuple remains unchanged and the returned value is a tuple; do not depend on whether an exhaustive slice like t[:] is the same object as the original, because implementations may reuse the immutable tuple.

python
t = (1, 2, 3, 4) s = t[1:3] print(s) # (2, 3) print(t) # (1, 2, 3, 4) - unchanged

This behavior differs from basic array slicing in NumPy, which returns a view by default. If you need to modify the extracted data, convert the slice to a list or another mutable sequence first; the slice itself is a tuple and cannot be altered.

Common Mistakes and Edge Cases

A step of zero raises ValueError: slice step cannot be zero. This is a common mistake when a computed step value accidentally becomes 0.

Out-of-range indices are silently clamped to the tuple boundaries. t[5:10] on a 5-element tuple returns an empty tuple, not an error. Similarly, t[-100:100] returns the entire tuple because the negative start is clamped to 0 and the positive stop is clamped to the length.

python
t = (1, 2, 3) print(t[5:10]) # () print(t[-100:100]) # (1, 2, 3)

When using a negative step, the stop index is still exclusive, but the traversal moves backward. This often leads to confusion about whether the stop element is included. For example, t[3:0:-1] includes index 3, 2, 1, but not 0.

python
t = (0, 1, 2, 3, 4) print(t[3:0:-1]) # (3, 2, 1)

To include index 0 in a reverse slice, omit the stop: t[3::-1] gives (3, 2, 1, 0).

Performance and Memory Considerations

When a tuple slice creates a new tuple, it copies the selected elements, so time and memory are O(k), where k is the number of elements in the slice. For large tuples, repeatedly extracting different portions can add up, so if profiling shows slicing is a bottleneck, consider iterating with itertools.islice instead of creating intermediate tuples. However, islice does not support negative steps or negative start/stop values, so it is not a drop-in replacement for all tuple slices.

python
from itertools import islice t = (0, 1, 2, 3, 4, 5) # Equivalent to t[2:5] print(tuple(islice(t, 2, 5))) # (2, 3, 4)

For most applications, the copy cost is negligible compared to the clarity of using slicing. Optimize only when profiling shows that tuple slicing is a bottleneck.

Practical Use Cases

Tuple slicing is useful for extracting fixed-size chunks from tuple-based data, such as coordinates or configuration values. For example, splitting a four-component coordinate tuple into two pairs:

python
point = (10, 20, 30, 40) x, y = point[:2] z, w = point[2:]

Reversing a tuple with [::-1] is a concise way to iterate in reverse order without converting to a list. This is particularly useful when you need to process elements from the end while preserving the tuple type.

Finally, tuple slicing and concatenation can rotate a tuple by n positions:

python
def rotate(t, n): if not t: return t n %= len(t) return t[n:] + t[:n] print(rotate((1, 2, 3, 4, 5), 2)) # (3, 4, 5, 1, 2)

This pattern relies on slicing to split the tuple and then concatenates the two slices. Because both operations return tuple-valued results, the final value is a tuple with the desired rotation.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.