question about slicing with a step length

Terry Reedy tjreedy at udel.edu
Thu Mar 9 20:42:32 EST 2006


"André" <andre.roberge at gmail.com> wrote in message 
news:1141906509.952096.165310 at p10g2000cwp.googlegroups.com...

>Terry Reedy wrote:
>> 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).

> It's not simply "+1".

Yes it is.  Boundary adjustment follows, as implied by what I wrote.

> >>> a = range(10)
> >>> a[9:0:-2]
> >>> a[10:0:-2]
> >>> a[11:0:-2]
> >>> a[42:0:-2]

are all [9, 7, 5, 3, 1] because a[1:10] == a[1:11] == a[1:12] == a[1:43] == 
[1,3,5,7,9], which is then reversed.

Terry Jan Reedy








More information about the Python-list mailing list