question about slicing with a step length

John Salerno johnjsal at NOSPAMgmail.com
Wed Mar 8 20:14:21 EST 2006


James Stroud wrote:
> 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]?
>>
>> I thought the first index, whether going forward or backward, was 
>> inclusive. And there is no index of 10 in this list, so what is it 
>> referring to?
>>
>> Thanks.
> 
> Its referring to right after the end of the list, just as the 0 refers 
> to just before the start of the list. The word "slice" is supposed to 
> suggest cutting between (or before or after) elements.
> 

But why isn't the first index inclusive in this case?



More information about the Python-list mailing list