4
  1. Is there any difference between mylist[:] and mylist[::]?
  2. 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

3 Answers 3

8
  1. No. Both result in slice(None, None, None).

  2. 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
Sign up to request clarification or add additional context in comments.

2 Comments

You could perhaps add an explanation about slice objects. At least see help(slice) :)
actually I would expect to get either a None object or a 0 length array back on a 0 length slice
1
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

1 Comment

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.
0

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

3 Comments

They can be 0, it's just that a 0 stride is meaningless.
@IgnacioVazquez-Abrams, >>> [1,2,3,4,5][::0] ValueError: slice step cannot be zero
That exception comes from list, not slice.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.