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?

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?

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.

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

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

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.