- Is there any difference between
mylist[:]andmylist[::]? - What's the rationale for
mylist[::0]to raise an error since negative steps are allowed?
Dan D.
75k15 gold badges111 silver badges129 bronze badges
asked Feb 15, 2012 at 7:27
ChessMaster
5491 gold badge4 silver badges12 bronze badges
3 Answers 3
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?
answered Feb 15, 2012 at 7:31
Ignacio Vazquez-Abrams
804k160 gold badges1.4k silver badges1.4k bronze badges
Sign up to request clarification or add additional context in comments.
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.
answered Feb 15, 2012 at 7:33
avasal
14.9k4 gold badges33 silver badges49 bronze badges
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
answered Feb 15, 2012 at 7:33
NPatel
21.4k17 gold badges104 silver badges165 bronze badges
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.lang-py