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?
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
Videos
Just use the slice and reverse it.
Copya[2:4] = a[2:4][::-1]
a[2:4] creates a copy of the selected sublist, and this copy is reversed by a[2:4].reverse(). This does not change the original list. Slicing Python lists always creates copies -- you can use
Copyb = a[:]
to copy the whole list.
The syntax is always [start:end:step] so if you go backwards your start needs to be greater than the end. Also remember that it includes start and excludes end, so you need to subtract 1 after you swap start and end.
l[5:2:-1]= [6, 5, 4]
l[4:1:-1]= [5, 4, 3]
Slice notation is [start:stop:step]. This means "begin at start, then increase by step until you get to end." It's similar to this construct:
counter = 0
stop = 10
step = 1
while counter < stop:
print(counter)
counter += step
This will produce, as expected, 0 through 9.
Now imagine if we tried it with the values you're using:
counter = 2
stop = 5
step = -1
while counter < stop:
print(counter)
counter += step
If we actually executed this, it would print 2, then add -1 to that to get 1, then print 1 and add -1 to that to get 0, then it would be -1, -2, and so on, forever. You would have to manually halt execution to stop the infinite loop.
If you leave start or stop empty, as in [:4] or [::-1], this indicates the beginning or end of the sequence, as determined by the step. Python will go forwards with a positive step and backwards with a negative step (trying to use a step of 0 produces an error).
>>> l[2::]
[3, 4, 5, 6, 7, 8]
>>> l[2::-1]
[3, 2, 1]
>>> l[:2:]
[1, 2]
>>> l[:2:-1]
[8, 7, 6, 5, 4]
If you specify a start, end, and step that couldn't work (an empty step defaults to 1), Python will simply return an empty sequence.
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 [].