Slicing in Sequence
The Pythonic convention of excluding the last item in slices and ranges works well with the zero-based indexing used in Python, C and many other languages.
The stride/step can also be negative, returning items in reverse
The [ ]
operator can also take multiple indexes or slices separated by commas. The __getitem__
and __setitem__
special methods that handle the [] operator simply receive the indices in a[i, j] as a tuple. In other words, to evaluate a[i, j]
, Python calls a.__getitem__((i, j))
.
Multidimensional Slicing and Ellipsis
Last updated
Was this helpful?