# [docs] [issue1446619] extended slice behavior inconsistent with docs

Steven Bethard report at bugs.python.org
Sun Mar 27 14:47:53 CEST 2011

```Steven Bethard <steven.bethard at gmail.com> added the comment:

The problem still exists in current trunk:

The slicing semantics have been removed from the expressions reference:
http://docs.python.org/py3k/reference/expressions.html#slicings

The datamodel and types sections still have the same equations:
http://docs.python.org/py3k/reference/datamodel.html#types
http://docs.python.org/py3k/library/stdtypes.html#typesseq

Here's a synopsis of the problem, in code:

>>> # positive step, works as described
>>> i, j, k = 2, 8, 2
>>> range(10)[i:j:k]
[2, 4, 6]
>>> [i + n * k for n in range(0, (j - i) / k)]
[2, 4, 6]
>>> [range(10)[i] for i in _]
[2, 4, 6]

>>> # negative step, does not work as described
>>> i, j, k = 10, 0, -2
>>> range(10)[i:j:k]
[9, 7, 5, 3, 1]
>>> [i + n * k for n in range(0, (j - i) / k)]
[10, 8, 6, 4, 2]
>>> [range(10)[i] for i in _]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range

>>> # actual behavior trims 10 to 9 (is the same as 9:0:-2)
>>> # that is, it trims to len(s) - 1, not len(s)
>>> range(10)[9:0:-2]
[9, 7, 5, 3, 1]

I propose to ignore the definition in the datamodel section, since it's talking about extended slicing in general, and doesn't mention anything about negative indices.

I propose the attached patch to fix the definition in the types section, which simply changes the statement:

If i or j is greater than len(s), use len(s)

to say:

If *i* or *j* is greater than len(s), use len(s) if *k* is positive,
or len(s) - 1 if *k* is negative.

----------
keywords: +patch
versions: +Python 2.7, Python 3.2, Python 3.3 -Python 2.6, Python 3.0