[issue28882] Slice confusing with negative stop and strides and the 0th element.

Martin Panter report at bugs.python.org
Fri Dec 9 18:42:07 EST 2016


Martin Panter added the comment:

I think Steven’s main complaint is that it is hard to make a reversed slice extend to the start of the original sequence, unless you omit (or use None as) the endpoint:

>>> "01234567"[4:0:-1]  # Includes index [4], stops before reaching index [0]
'4321'
>>> "01234567"[3::-1]  # Includes index [3], stop omitted for start of string
'3210'
>>> "01234567"[3:None:-1]  # None means the same
'3210'
>>> "01234567"[3:-1:-1]  # Negative means len(...) - 1, i.e. index [7]
''

This is a consequence of (a) the stop parameter meaning “stop _before_ reaching this index”, and (b) negative indexes are interpreted specially as being relative to the end.

----------
nosy: +martin.panter

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28882>
_______________________________________


More information about the Python-bugs-list mailing list