29

The extended slice syntax in python has been explained to me as "a[n:m:k] returns every kth element from n to m".

This gives me a good idea what to expect when k is positive. But I'm lost on how to interpret a[n:m:k] for negative k. I know that a[::-1] reverses a, and that a[::-k] takes ever kth element of the reversed a.

But how is this a generalization of the definition for k positive? I'd like to know how a[n:m:k] is actually defined, so that (for example) I can understand why:

"abcd"[-1:0:-1] = "dcb"

Is a[n:m:-k] reversing the sequence a, then taking the elements with original indices starting from n and ending one before m or something? I don't think so, because this pattern doesn't fit other values of n and m I've tried. But I'm at a loss to figure out how this is actually defined, and searching has gotten me nowhere.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Alex Becker
  • 433
  • 1
  • 4
  • 10
  • 1
    Related question: http://stackoverflow.com/questions/12521798/what-are-the-default-slice-indices-in-python-really – user2314737 Aug 27 '16 at 22:17
  • This question aint a duplicate of "Explain Python's slice notation". This one asks specifics about negative step whereas the latter is generic to slice – gawkface May 18 '17 at 05:36

2 Answers2

24

[-1:0:-1] means: start from the index len(string)-1 and move up to 0(not included) and take a step of -1(reverse).

So, the following indexes are fetched:

le-1, le-1-1, le-1-1-1  .... 1  # le is len(string)

example:

In [24]: strs = 'foobar'

In [25]: le = len(strs)

In [26]: strs[-1:0:-1]  # the first -1 is equivalent to len(strs)-1

Out[26]: 'raboo'

In [27]: strs[le-1:0:-1]   
Out[27]: 'raboo'
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 1
    Thank you, this explanation was very clear to me. I was missing the recursive stepping and the fact that the endpoint is not inclusive. – Alex Becker Jan 25 '13 at 08:09
  • Yups, the point that step creates a sequence makes this explanation "graspable" i.e. str[-2:-6:-1] will result in following indices being selected -2, -2-1=-3, -2-1-1=-4, all the way to -6 (excluded). Eliciting with a string: >>> print ("07654321"[-2:-6:-1]) 2345 – gawkface May 18 '17 at 05:46
  • What value would you explicitly use for `i` and `j` in `strs[i:j:-1]` to reverse a string like `strs[::-1]`? – Asad Moosvi Aug 15 '17 at 12:22
  • @AsadMoosvi `i` could be `None` or `len(strs)` and `j` could be `None`. – Ashwini Chaudhary Aug 15 '17 at 13:11
  • very nice thanks. Would strs[-2:-6] = 'ooba' and strs[-2:-6:-1] = 'aboo' – trustory Feb 15 '19 at 18:33
12

The Python documentation (here's the technical one; the explanation for range() is a bit easier to understand) is more correct than the simplified "every kth element" explanation. The slicing parameters are aptly named

slice[start:stop:step]

so the slice starts at the location defined by start, stops before the location stop is reached, and moves from one position to the next by step items.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561