3

When s = 'abcdefg', s[0:4:-1] and s[1:-1:-1] return nothing in Python3. However, s[:4:-1] returns "gf". Could anyone explain the mechanism behind these two scenarios (especially in terms of memory)?

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
Sid Lin
  • 33
  • 3
  • 1
    You might want to ready this https://stackoverflow.com/questions/509211/understanding-slice-notation – Olivier Melançon Aug 30 '19 at 13:19
  • Possible duplicate of [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – DjaouadNM Aug 30 '19 at 13:20
  • I world argue it's not a perfect duplicate since it des not specifically exploits OP's cornercase – Olivier Melançon Aug 30 '19 at 13:21
  • Hi @OlivierMelançon, I've read the thread, and don't think there is answer for my question. MrGeek, as mentioned above, the thread can't answer why s[0:4:-1] return nothing, so I don't think this my question is duplicate. – Sid Lin Aug 30 '19 at 13:48
  • @SidLin It actually does but very briefly. Please see my answer for a complete explanation – Olivier Melançon Aug 30 '19 at 13:49

1 Answers1

2

The start is always the first index of the iteration.

The stop is the end-boundary of your iteration.

The step gives an orientation and a step size to the iteration. So the iteration always goes from start to stop in the direction given by the sign of step.

Thus s[start:stop:step] means to take the characters s[start], s[start + step], s[start + 2 * step], ..., and so on until stop is reached.

In other words, s[start:stop:-1] is not equivalent to reversed(s[start: stop]).

Examples

Thus in your example s[0:4:-1], the is step negative so the iteration process immediately stops because the start index is already on the left of stop.

a b c d e f g h
0 1 2 3 4 5 6 7
^       ^
s       s
t       t
a       o
r       p
t

<-------- orientation

On the other end, if you were to try s[4:0:-1], you would get 'edcb' (remember that the stop index is excluded).

a b c d e f g h
0 1 2 3 4 5 6 7
^       ^
s       s
t       t
o       a
p       r
        t

<-------- orientation

Finally, leaving start empty means from the end of the string. This is why you see all characters from the end to index 4 in reverse.

a b c d e f g h
0 1 2 3 4 5 6 7
        ^     ^
        s     s
        t     t
        o     a
        p     r
              t

<-------- orientation
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
  • 1
    Note that aside from the canonical reversing slice (`[::-1]`) it's usually harder to interpret a slice running in reverse, so even though it's slightly less efficient, it's often a good idea to perform your slice as a forward slice, then reverse it, e.g. `x[:4][::-1]` rather than the faster, but harder to read equivalent, `x[3::-1]`. – ShadowRanger Aug 30 '19 at 14:07
  • @ShadowRanger Actually, I agree with the readability issue. I'd even add that I usually prefer the even more readable `reversed(x[:4])` when an iterator is sufficient. – Olivier Melançon Aug 30 '19 at 17:45