-1

So I understand that Python has extended slicing which will grab substring with [start:end:step], with default values for start and end.

So for example:

L = range(6)
>>>L[::2]
[0, 2, 4, 6]

Okay makes sense... no value for start so it defaults to 0, then increments by 2.

>>>L[::-1]
[6, 4, 2, 0]

Now this is where i'm really confused. No value for start so it should be 0, but it starts at back of list... ?? Shouldn't it be [0, 6, 4, 2]? Why does step = -1 give special behavior for starting at back of list?

mgilson
  • 300,191
  • 65
  • 633
  • 696
alexkim
  • 36
  • 6

1 Answers1

8

It is documented along with everything else here (emphasis added):

s[i:j:k]

If i or j are omitted or None, they become “end” values (which end depends on the sign of k).

Community
  • 1
  • 1
BrenBarn
  • 242,874
  • 37
  • 412
  • 384