This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: slice().indices() returns incorrect values
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: duplicate
Dependencies: Superseder: slice.indices with negative step and default stop
View: 11842
Assigned To: Nosy List: mark.dickinson, misianne
Priority: normal Keywords:

Created on 2020-11-25 20:54 by misianne, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg381862 - (view) Author: misianne (misianne) Date: 2020-11-25 20:54
I have a problem with slice().indices():

x=[0,1,2,3,4,5,6,7,8,9]*10
x[-91:-101:-3] # == [9, 6, 3, 0] OK!
x[slice(*slice(-91,-101,-3).indices(len(x)))] # == [] WRONG!

This is correct:

x[-91:-100:-3] # == [9, 6, 3] OK!
x[slice(*slice(-91,-100,-3).indices(len(x)))] # == [9, 6, 3] OK!

This is not:

x[-91:-101:-3] # == [9, 6, 3, 0] OK!
x[slice(*slice(-91,-101,-3).indices(len(x)))] # == [] WRONG!

The problem is that 
	slice(-91,-101,-3).indices(len(x))
returns:
	(9,-1,-3)
which result in an empty list, while:
	(9,None,-3)
gives the correct result.
msg381864 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-11-25 21:10
This is essentially a duplicate of #11842. In short, slice.indices is working as intended here. It's best to think of slice.indices as start, stop and step arguments that could be provided to the range built-in function to get the set of relevant indices.
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86633
2020-11-25 21:10:34mark.dickinsonsetstatus: open -> closed

superseder: slice.indices with negative step and default stop

nosy: + mark.dickinson
messages: + msg381864
resolution: duplicate
stage: resolved
2020-11-25 20:54:21misiannecreate