Why does list[::-1] not equal list[:len(list):-1]?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Breezy Bay
--
Chapters
00:00 Question
01:22 Accepted answer (Score 6)
02:09 Answer 2 (Score 2)
02:36 Thank you
--
Full question
https://stackoverflow.com/questions/3690...
Question links:
[Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.]: https://docs.python.org/2/tutorial/intro...
Accepted answer links:
[sequence types]: https://docs.python.org/2/library/stdtyp...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #slice
#avk47
ACCEPTED ANSWER
Score 6
The documentation you're referencing is the tutorial, which gives only an informal overview of Python syntax and semantics. It doesn't explain all the details. You'll note that the tutorial page you linked to doesn't even discuss negative indices.
The actual documentation is given in the library reference under sequence types. Although it is a bit terse and not easy to understand on a first read, it does clarify that for a slice [i:j:k]:
If i or j are omitted or None, they become “end” values (which end depends on the sign of k).
ANSWER 2
Score 2
l[::-1] is the same thing as l.__getitem__(slice(None, None, -1)). Since the start and the stop are both None, the list will be traversed from one end to the other. The step argument determines the direction as well as the step.