I thought I understood the basics of list slicing in python, but have been receiving an unexpected error while using a negative step on a slice, as follows:
>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[:-1]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> a[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> a[:-1:-1]
[]
(Note that this is being run in Python 3.5)
Why doesn't a[:-1:-1] reverse step through the a[:-1] slice in the same manner as it does through the whole list with a[::-1]?
I realize that you can use list.reverse() as well, but trying to understand the underlying python slice functionality better.
6 Answers
The first -1 in a[:-1:-1] doesn't mean what you think it does.
In slicing, negative start/end indices are not interpreted literally. Instead, they are used to conveniently refer to the end of the list (i.e. they are relative to len(a)). This happens irrespectively of the direction of the slicing.
This means that
a[:-1:-1]
is equivalent to
a[:len(a)-1:-1]
When omitted during reverse slicing, the start index defaults to len(a)-1, making the above equivalent to
a[len(a)-1:len(a)-1:-1]
This always gives an empty list, since the start and end indices are the same and the end index is exclusive.
To slice in reverse up to, and including, the zeroth element you can use any of the following notations:
>>> a[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> a[:None:-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> a[:-len(a)-1:-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
When you type [1, 2, 3, ...][1:4:1] it is the same as [1, 2, 3, ...][slice(1, 4, 1)]. So 1:4:1 is the shorthand for slice object. slice signature is slice(stop) or slice(start, stop[, step]) and you can also use None for arguments.
:: -> slice(None, None, None)
:4 -> slice(4)
# and so on
Suppose we have got [a: b: c]. Rules for indices will be as follows:
- First
cis checked. Default is+1, sign ofcindicates forward or backward direction of the step. Absolute value ofcindicates the step size. - Than
ais checked. Whencis positive orNone, default forais0. Whencis negative, default forais-1. - Finally
bis checked. Whencis positive orNone, default forbislen. Whencis negative default forbis-(len+1).
Note 1: Degenerated slices in Python are handled gracefully:
- the index that is too large or too small is replaced with
lenor0. - an upper bound smaller than the lower bound returns an empty list or string or whatever else (for positive
c).
Note 2: Roughly speaking, Python picks up elements while this condition (a < b) if (c > 0) else (a > b) is True (updating a += c on every step). Also, all negative indices are replaced with len - index.
If you combine this rules and notes it will make sense why you got an empty list. In your case:
In[1]: [1, 2, 3, 4, 5, 6][:-1:-1] # `c` is negative so `a` is -1 and `b` is -1
Out[1]: []
# it is the same as:
In[2]: [1, 2, 3, 4, 5, 6][-1: -1: -1] # which will produce you an empty list
Out[2]: []
There is very good discussion about slice notation: Explain Python's slice notation!
I generally find it useful to slice a range-object (this is only possible in python3 - in python2 range produces a list and xrange can't be sliced) if I need to see which indices are used for a list of a given length:
>>> range(10)[::-1]
range(9, -1, -1)
>>> range(10)[:-1]
range(0, 9)
And in your last case:
>>> range(10)[:-1:-1]
range(9, 9, -1)
This also explains what happened. The first index is 9, but 9 isn't lower than the stop index 9 (note that in python the stop index is excluded) so it stops without giving any element.
Note that indexing can also be applied sequentially:
>>> list(range(10))[::-1][:-1] # first reverse then exclude last item.
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> list(range(10))[:-1][::-1] # other way around
[8, 7, 6, 5, 4, 3, 2, 1, 0]
Python's slices seem fairly simple at first, but their behaviour is actually quite complex (notes 3 and 5 are relevant here). If you have a slice a[i:j:k]:
- If
iorjare negative, they refer to an index from the end ofa(soa[-1]refers to the last element ofa) If
iorjare not specified, or areNone, they default to the ends ofa, but which ends depends on the sign ofk:- if
kis positive, you're slicing forwards, soibecomes 0 andjbecomeslen(a) if
kis negative, you're slicing backwards, soibecomeslen(a)andjbecomes the element before the start ofa.NB:
jcannot be replaced with -1, since doing that will cause Python to treatjas the last element ofarather than the (nonexistent) element beforea[0]. To get the desired behaviour, you must use-len(a)-1(or-(len(a)+1)) in place ofj, which means that to get toa[j], slice starts at the last element ofa, goes left forlen(a)elements and then left one more element, ending up beforeastarts and thus includinga[0]in the slice.
- if
Therefore, a[:-1:-1] means "go from the end of a, which is a[-1] (since i is unspecified and k is negative), to the last element of a (since j == -1), with step size of -1". i and j are equal – you start and stop slicing in the same place – so the expression evaluates to an empty list.
To reverse a[:-1], you can use a[-2::-1]. This way, the slice starts at the penultimate element, a[-2] (since a[:-1] does not include a[-1]) and goes backwards until the element "before" a[0], meaning that a[0] is included in the slice.
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a[:-1]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> a[-2::-1]
[8, 7, 6, 5, 4, 3, 2, 1, 0]
slice works similar to range in that when you make the step argument a negative number, the start and stop arguments work in the opposite direction.
>>> list(range(9, -1, -1)) == a[::-1]
True
Some examples that may help make this more clear:
>>> a[6:2:-2]
[6, 4]
>>> a[0:None:1] == a[::]
True
>>> a[-1:None:-1] == a[::-1]
True
>>> a[-2:None:-1] == a[:-1][::-1]
True
In a simple way understand that if a[::-1] -1 at the end reverses the string.
Now
a=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
a[::-1]=[9, 8, 7, 6, 5, 4, 3, 2, 1, 0];
now a[:-1:-1] -1 in the middle doesn't make any sense because now it's the first element and this will give an empty list.
Whereas a[-1::-1] gives you completed list and makes sense.