question about slicing with a step length

Terry Reedy tjreedy at udel.edu
Wed Mar 8 23:50:12 EST 2006


> John Salerno wrote:
>> Given:
> > numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> > can someone explain to me why
> > numbers[10:0:-2] results in [10, 8, 6, 4, 2]?

It appears that s[i:j:-1] is s[(j+1):(i+1)] .reverse()'ed.  For 'numbers',
this is 10, 9, 8, 7, 6, 5, 4, 3, 2].  Then take every other item.  Why the 
+1?  Don't know and not my intuitive expectation.  I just know that 
extended slicing was developed for and until recently pretty much 
restricted to numeric (now numpy).

Steven D'Aprano
> I think the documentation is misleading/incomplete when
> it comes to negative strides for extended slices.

and Andre "Agreed!" also, and you three aren't the only ones.  Maybe some 
day I will read the source, think about it more, and post a suggested 
revision for comment  ... or maybe someone will beat me to it.

Terry Jan Reedy






More information about the Python-list mailing list