For an explanation of the results of your various attempts, see Matthew anser .
Between the lines of your question, though, I read that you want to replicate the behavior of a[::-1] , but explicitly passing in the start and stop slice.
The Python tutorial will take a closer look at the lines that mention this useful mnemonics:
One way to remember how slices work is to consider indices as pointing between characters, with the left edge of the first character being numbered 0. Then the right edge of the last character is a string of n characters with index n, for example:
+---+---+---+---+---+---+ | P | y | t | h | o | n | +---+---+---+---+---+---+ 0 1 2 3 4 5 6 -6 -5 -4 -3 -2 -1
The first line of numbers gives the position of the indices 0 ... 6 in Struna; the second line gives the corresponding negative indices. The slice from i to j consists of all the characters between the edges indicated respectively by i and j.
Of course, the same line rules apply to other sequences, such as lists and tuples. The textbook does not mention [1] this means that for negative steps you need to work with shifted indices indicating between For example:
βββββ°ββββ°ββββ°ββββ°ββββ°ββββ β P β y β t β h β o β n β <== For backwards slicing (ie negative step) βββββββββββββββββββββββββ¦ 0 1 2 3 4 5 -7 -6 -5 -4 -3 -2 -1
This is because indexes do not actually indicate between elements of a sequence. Rather, the cut stop parameter is an exceptional boundary, i.e. The resulting sequence ends with the last element spanned by step , before who has the stop index.
From this shifted pseudo-index table, it can be seen that in order for the whole sequence to be reversed by cutting with explicit boundaries, we must start with len(a) - 1 (or higher). We also see that there is no non-negative index that we can use as stop , and still have the first element of the original sequence, included as the last element of the resulting sequence. [len(a)-1:0:-1] will end already in the second source element. And we cannot use -1 as stop , as it is a short hand to refer to the position after the last element independent of len(a) . We should use -len(a) - 1 as stop (or lower) instead:
>>> a[len(a)-1:-len(a)-1:-1] [5, 4, 3, 2, 1]
If you explicitly specify the boundaries explicitly, this is probably why they can be adapted if only a part of the sequence should be used in a later version of the code. In order for the code to be adapted, it is necessary to revise the current audit so that you can make an informed decision about what needs to be changed. Needless to say, a[len(a)-1:-len(a)-1:-1] much less clear than list(reversed(a)) , which can easily be supplemented with borders:
>>> list(reversed(a[2:4])) # referring to indices of original sequence [4, 3] >>> list(reversed(a))[1:3] # referring to indices of reversed sequence [4, 3]
[1] The reason why this tutorial does not mention this is probably before Python 2.2 the possibility of using a step where cutting can only be used for NumPy sequences (and possibly other third-party sequences), but is not supported by the list of Python built-in sequences, tuple and string. Python 2.3 added an implementation of this extended slice semantics for built-in sequence types.