- Is there any difference between
mylist[:]andmylist[::]? - What's the rationale for
mylist[::0]to raise an error since negative steps are allowed?
3 Answers
No. Both result in
slice(None, None, None).Positive strides go forwards. Negative strides go backwards. Zero strides go... nowhere? How exactly would that work? An infinite sequence of a single value?
2 Comments
Kimvais
You could perhaps add an explanation about
slice objects. At least see help(slice) :)Chris Rudd
actually I would expect to get either a None object or a 0 length array back on a 0 length slice
No difference between mylist[:] and mylist[::]
mylist[::0]
This implies from starting index to last index without any step, don't know in what world it would be possible.
1 Comment
Open AI - Opting Out
The explicit zero makes you think, "No-one would ever do that", but if the value had been calculated instead it wouldn't be possible to spot, and I can imagine it happening.
Third element is for steps. When you write mylist[:] it will assume step will be 1 which is same case in mylist[::].
If you write mylist[::0] then it will raise error because steps can be +ve or -ve not 0
3 Comments
Ignacio Vazquez-Abrams
They can be 0, it's just that a 0 stride is meaningless.
warvariuc
@IgnacioVazquez-Abrams, >>> [1,2,3,4,5][::0] ValueError: slice step cannot be zero
Ignacio Vazquez-Abrams
That exception comes from
list, not slice.