Question: Python list error: [::-1] step on [:-1] slice

Question

Python list error: [::-1] step on [:-1] slice

Answers 5
Added at 2017-01-02 18:01
Tags
Question

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.

Answers
nr: #1 dodano: 2017-01-02 18:01

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]
nr: #2 dodano: 2017-01-02 18:01

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 i or j are negative, they refer to an index from the end of a (so a[-1] refers to the last element of a)
  • If i or j are not specified, or are None, they default to the ends of a, but which ends depends on the sign of k:

    • if k is positive, you're slicing forwards, so i becomes 0 and j becomes len(a)
    • if k is negative, you're slicing backwards, so i becomes len(a) and j becomes the element before the start of a.

      NB: j cannot be replaced with -1, since doing that will cause Python to treat j as the last element of a rather than the (nonexistent) element before a[0]. To get the desired behaviour, you must use -len(a)-1 (or -(len(a)+1)) in place of j, which means that to get to a[j], slice starts at the last element of a, goes left for len(a) elements and then left one more element, ending up before a starts and thus including a[0] in the slice.

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]
nr: #3 dodano: 2017-01-02 19:01

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:

  1. First c is checked. Default is +1, sign of c indicates forward or backward direction of the step. Absolute value of c indicates the step size.
  2. Than a is checked. When c is positive or None, default for a is 0. When c is negative, default for a is -1.
  3. Finally b is checked. When c is positive or None, default for b is len. When c is negative default for b is -(len+1).

Note 1: Degenerated slices in Python are handled gracefully:

  • the index that is too large or too small is replaced with len or 0.
  • 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!

nr: #4 dodano: 2017-01-02 22:01

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
nr: #5 dodano: 2017-01-02 23:01

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]
Source Show
◀ Wstecz