Slice notation in short:
[ <first element to include> : <first element to exclude> : <step> ]
If you want to include the first element when reversing a list, leave the middle element empty, like this:
foo[::-1]
You can also find some good information about Python slices in general here:
Explain Python's slice notation
list[::-1]
I learned this years ago but never thought about it until reviewing just now. Why is it
[::-1]
? Does the first colon(:) mean we start at the beginning, and the second colon(:) mean we end also at the beginning?
Videos
Slice notation in short:
[ <first element to include> : <first element to exclude> : <step> ]
If you want to include the first element when reversing a list, leave the middle element empty, like this:
foo[::-1]
You can also find some good information about Python slices in general here:
Explain Python's slice notation
If you are having trouble remembering slice notation, you could try doing the Hokey Cokey:
[In: Out: Shake it all about]
[First element to include: First element to leave out: The step to use]
YMMV
Let's take a list for example,
a = [1, 2, 3, 4, 4, 5, 6, 9]
If you try to slice it using positive indices,
newa = a[1:5]
This will result in
newa = [2, 3, 4, 4]
This is because, in the above case slicing occurs like this, a[inclusive : exclusive], first index is included and slicing begins from this index, and end just one index before the index(5), exclusive(remember). This is just the way how list slicing works.
To get last two values of the list a,
newa = [6 : ]
Here, the index starts from 6(inclusive) and ends till end of the list.
If you are keen in using negative indexing in lists as Python offers to ease up indexing, know once and for all the inclusive and exclusive thing about slicing.
For same example above, to get last two numbers of the list using negative index, what we do is,
newa = a[-2 : ]
In the case of positive indexing, the index begins at 0 while for negative indexing, the index is at -1, -2 and so on. So in the above example, starting from second last number in the list, slice till the end of the list, is what is understood.
Now, to reverse the list, one could do,
print a.reverse()
[9, 6, 5, 4, 4, 3, 2, 1]
In slicing way, list can be reversed by giving it a [start, end, step] like mentioned above, but I would like to clarify it further.
r = a[2: : -1]
This will make a new list starting with number from index 2, and till the end of the list, but since the step is -1, we decrease from index 2, till we reach 0. Hence we come up with a reversed list of first three numbers as below:
r = [3, 2, 1]
Also, to get a whole of the list as reversed,
r = a[len(a): : -1]
This will start from 8, the length of the list, but in indices terms which begins from 0, we have 0 till 8 indices, so starting from 8th indices, it goes on decreasing in steps of -1, till the end of the list. The result is as:
[9, 6, 5, 4, 4, 3, 2, 1]
I personally prefer to use this for reversing the list.
For the Python list slicing syntax, list[start:end:step] will get a sliced list with items starting with list[start], but list[end] is excluded. As a result, in your case, L[10:0:-1] will exclude L[0], i.e., 0, and L[10::-1] will work as you expect.
When the start or end is a negative number, it means it counts from the end of the list. So list[-1] will get the last item in the list. In your case, L[10:-1:-1] is equivalent to L[10:10:-1]. So L[10:-1:-1] will get [].