Reputation: 1904
If I have an array a
, I understand how to slice it in various ways. Specifically, to slice from an arbitrary first index to the end of the array I would do a[2:]
.
But how would I create a slice object to achieve the same thing? The two ways to create slice objects that are documented are slice(start, stop, step)
and slice(stop)
.
So if I pass a single argument like I would in a[2:]
the slice
object would interpret it as the stopping index rather than the starting index.
Question: How do I pass an index to the slice
object with a starting index and get a slice object that slices all the way to the end? I don't know the total size of the list.
Upvotes: 9
Views: 5751
Reputation: 155428
Use None
everywhere the syntax-based slice
uses a blank value:
someseq[slice(2, None)]
is equivalent to:
someseq[2:]
Similarly, someseq[:10:2]
can use a preconstructed slice
defined with slice(None, 10, 2)
, etc.
Upvotes: 19