question about slicing with a step length

James Stroud jstroud at ucla.edu
Wed Mar 8 20:44:26 EST 2006


John Salerno wrote:
> 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?
You skipped it with the -2 step size.

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list