Slice Methods
slice.indices()
Normalize a slice against a sequence length, returning integer start, exclusive stop and nonzero step. Missing bounds, negative indices and out-of-range bounds follow ordinary slice rules; negative steps use different defaults. The normalized triple can be passed to range() to enumerate positions. Do not blindly reconstruct a slice from it: a normalized stop of -1 for reverse traversal is a sentinel, but a new slice would interpret -1 relative to the sequence length. Negative lengths or zero steps raise ValueError; non-indexable bounds or lengths raise TypeError.
Formal Syntax & Structure
python syntaxpython
selection.indices(length, /)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| length | int | SupportsIndex | Nonnegative length of the target sequence. |
Return Value
tuple[int, int, int]
Normalized start, stop and step for range().
Working Example
Example Snippetpython
items = ["a", "b", "c", "d", "e"]s = slice(None, None, -2)positions = s.indices(len(items))print(positions)print([items[i] for i in range(*positions)])print(items[s])