- Is there any difference between
mylist[:]
andmylist[::]
? - What's the rationale for
mylist[::0]
to raise an error since negative steps are allowed?
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?
-
1You could perhaps add an explanation about
slice
objects. At least seehelp(slice)
:) – Kimvais Feb 15 '12 at 7:34
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.
-
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. – Peter Wood Feb 15 '12 at 8:16
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
-
1They can be 0, it's just that a 0 stride is meaningless. – Ignacio Vazquez-Abrams Feb 15 '12 at 7:35
-
1@IgnacioVazquez-Abrams, >>> [1,2,3,4,5][::0] ValueError: slice step cannot be zero – warvariuc Feb 15 '12 at 8:06
-