Python Extended Slicing: Step and Negative Indexes
Understand Python extended slicing with the step parameter, negative indexes, slice objects, and copy behavior for lists and custom containers.

Python extended slicing refers to the three-part slice syntax seq[start:stop:step]. The third component, step, distinguishes it from the basic two-part form, and it changes how elements are selected in ways that are easy to misread at first glance.
The Step Parameter and How It Works
The step determines the stride between selected elements. seq[1:8:2] picks every second element starting at index 1 and stopping before index 8.
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[1:8:2]) # [1, 3, 5, 7]The start and stop positions follow the same half-open rule as basic slicing: the element at start is included, and the element at stop is not. The step only controls which indices between those bounds are visited.
When step is positive, the slice moves from left to right. When step is negative, the direction reverses, and the meaning of start and stop also reverses.
Negative Steps and Reversing Sequences
A negative step makes Python traverse the sequence backward. seq[::-1] is the canonical way to reverse a list.
numbers = [0, 1, 2, 3, 4, 5]
print(numbers[::-1]) # [5, 4, 3, 2, 1, 0]With a negative step, an omitted start begins at the end of the sequence and an omitted stop continues through index 0. If both are omitted, as in [::-1], the result is the whole sequence reversed.
For a negative step, start must be greater than stop for the slice to produce any elements. seq[5:1:-1] begins at index 5 and moves down to index 2, because the stop index is exclusive here as well.
numbers = [0, 1, 2, 3, 4, 5]
print(numbers[5:1:-1]) # [5, 4, 3, 2]A common mistake is assuming that seq[0:5:-1] returns a reversed prefix. It returns an empty list, because index 0 is not greater than index 5, so no indices satisfy the traversal direction.
Slice Objects and the __getitem__ Protocol
When Python evaluates seq[1:8:2], it constructs a slice object and passes it to the sequence's __getitem__ method. The slice object has three attributes: start, stop, and step.
s = slice(1, 8, 2)
print(s.start, s.stop, s.step) # 1 8 2You can create slice objects explicitly and reuse them. This is useful when the same slicing pattern appears in several places.
every_third = slice(0, None, 3)
data = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(data[every_third]) # [10, 40, 70]The None values in the slice object correspond to omitted bounds. Python resolves them against the sequence length at access time, which is why the same slice object can be applied to sequences of different lengths.
Memory Behavior: Copies, Not Views
A slice of a list produces a new list. The new list is a shallow copy: it contains references to the same elements, not deep copies of them. Rebinding an item in the slice does not affect the original.
original = [1, 2, 3, 4, 5]
subset = original[::2]
subset[0] = 99
print(original) # [1, 2, 3, 4, 5]
print(subset) # [99, 3, 5]If an element is itself mutable, mutating that object through the slice will be visible through the original list, because both lists hold the same reference.
This is different from NumPy arrays, where extended slicing returns a view that shares memory with the original array. The distinction matters when you write code that mutates the result of a slice and expect the change to propagate.
For large lists, allocating a new list can be expensive. If you only need to iterate over every second element, itertools.islice avoids building a new list, though it does not support negative steps.
from itertools import islice
for value in islice(data, 0, None, 2):
print(value)Common Edge Cases and Off-by-One Traps
The stop index is exclusive for both positive and negative steps, which causes the most frequent off-by-one errors.
letters = ['a', 'b', 'c', 'd', 'e']
print(letters[3:0:-1]) # ['d', 'c', 'b']Here the slice starts at index 3 and stops before index 0, so index 0 is excluded. To include the first element while traversing backward, omit the stop entirely: letters[3::-1].
print(letters[3::-1]) # ['d', 'c', 'b', 'a']A step of zero raises ValueError: slice step cannot be zero. This is the one step value that is rejected outright.
numbers[::0] # ValueError: slice step cannot be zeroOut-of-range bound values in a slice do not raise errors. They are clamped to the sequence bounds, which can silently produce shorter slices than expected.
Implementing Extended Slicing in Custom Classes
When you define a class that supports slicing, __getitem__ must handle both integer indices and slice objects.
class RingBuffer:
def __init__(self, items):
self._items = list(items)
def __getitem__(self, key):
if isinstance(key, slice):
return self._items[key]
if isinstance(key, int):
return self._items[key]
raise TypeError('Invalid index type')The slice object is passed as a single argument. You can inspect its start, stop, and step attributes, or use slice.indices(length) to convert the slice into a concrete range for the current sequence length.
class CircularBuffer:
def __init__(self, items):
self._items = list(items)
def __getitem__(self, key):
if isinstance(key, slice):
indices = range(*key.indices(len(self._items)))
return [self._items[i % len(self._items)] for i in indices]
return self._items[key % len(self._items)]slice.indices(length) resolves the slice against a given sequence length and returns a three-tuple (start, stop, step) normalized for use with range, including the exclusive stop value. Using it avoids reimplementing bound clamping and direction handling yourself.
s = slice(1, 8, 2)
print(s.indices(10)) # (1, 8, 2)
print(s.indices(3)) # (1, 3, 2)This is the most reliable way to handle extended slicing in a custom container, because it delegates the normalization rules to the standard library rather than duplicating them.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.