The Python slice object is the value that Python builds from the start:stop:step syntax inside square brackets. slice(1, 5, 2) creates the same object as the expression 1:5:2 does in items[1:5:2], and you can store it in a variable, pass it to functions and reuse it on many sequences. You use it when the same cut must be applied repeatedly, when you want to name a range for readability, or when you implement __getitem__ for your own class.
Every example below runs on Python 3.13 or 3.14 with the standard library only.
When you write data[2:6], Python does not pass two integers to the list. It builds a slice instance and calls data.__getitem__(slice(2, 6, None)). The built-in slice type has three read-only attributes: start, stop and step. Any of them can be None, which means "use the default for this position".
The constructor accepts one, two or three arguments. With one argument it is the stop value, so slice(3) equals [:3]. With two arguments they are start and stop. Use None to skip a position: slice(None, None, 2) equals [::2].
| Syntax | Equivalent slice call |
|---|---|
[:3] | slice(3) or slice(None, 3) |
[2:] | slice(2, None) |
[::2] | slice(None, None, 2) |
[::-1] | slice(None, None, -1) |
[1:8:3] | slice(1, 8, 3) |
Named slices make fixed-width or fixed-position parsing readable. Instead of repeating magic numbers, give each range a name once.
The same objects work on any sequence that supports slicing, including str, list, tuple, bytes, bytearray and range. That is useful when you process many lines with the same layout, or when you apply one slice to several parallel lists.
Slicing never raises IndexError for out of range bounds; it clamps them. Negative values count from the end, and a negative step walks backwards.
With a negative step, start must be greater than stop, otherwise the result is empty.
A step of 0 raises ValueError: slice step cannot be zero. The step can be any non-zero integer, or None.
A slice can describe a range without knowing the length of the sequence. The indices(length) method turns it into concrete, clamped (start, stop, step) integers for a sequence of that length. This is what you need when you write your own container.
Unpacking the result into range() gives you exactly the positions that the slice would select. Python's own sequences use the same logic internally.
If you define __getitem__, the argument is either an integer or a slice. Check the type and return the same kind of container for a slice.
If you build the result by hand instead of delegating to a list, use range(*index.indices(len(self))) to get the positions.
Slice objects are not hashable before Python 3.12. From Python 3.12 they are hashable, so they can be dictionary keys, provided their start, stop and step are hashable too.
Libraries such as NumPy accept tuples that mix integers and slices. A slice object can be placed in the tuple, which lets you build indexes dynamically.
Here grid[rows, cols] is the same as grid[1:3, ::2]. Use ... (Ellipsis) alongside slices when you want to skip leading dimensions. NumPy slices return views, while list slices return copies.
Yes. A list slice creates a new list with references to the same elements, so it is a shallow copy. items[:] is a common way to copy a list.
From Python 3.12 slice objects are hashable, so you can, if their parts are hashable. In older versions you get TypeError: unhashable type: 'slice'.
slice(3) means stop at 3, so it equals [:3]. slice(3, None) means start at 3 and go to the end, so it equals [3:].
Yes. Negative start and stop count from the end, and a negative step reverses direction. slice(None, None, -1) reverses a sequence.
A slice is a small object holding start, stop and step, and a[1:5:2] is shorthand for a[slice(1, 5, 2)]. Store slices in variables to name ranges, call .indices(length) to resolve them against a size, and branch on isinstance(index, slice) in your own __getitem__. Bounds are clamped rather than raising errors, and a step of zero is the only invalid value.