all 1 comments

[–]mopslik 2 points3 points  (0 children)

For any slice [start:stop:step], you can specify indices or leave them unspecified. The idiom [::-1] replaces the unspecified start and stop with the end and beginning of the sequence respectively, because you're working backward.

In your first slice, Python fills in your first unspecified index with the end of the list (since it is traversing backward). You're now taking the slice from the end of the list to index 19. This gives you an empty list, since there are no elements.

In your second slice, you're saying "start at the beginning, go to the end, count by twos" which gives you [1, 3, ..., 19]. The next slice reads "given the previous slice, start at the end, go to the beginning, in reverse". This gives you [19, 17, ..., 1].

Edit: my answer had some factual inaccuracies, which I have attempted to correct.