[Tutor] is the the two style of writting the same?

eryksun eryksun at gmail.com
Wed Aug 21 01:53:26 CEST 2013


On Tue, Aug 20, 2013 at 3:31 PM, Walter Prins <wprins at gmail.com> wrote:
> On 20 August 2013 10:20, sikonai sikonai <tanjyunda at gmail.com> wrote:
>>
>> >>> list=[1,2,3,4,5,6,7,8,9]
>> >>> list[9:0:-2]
>> [9, 7, 5, 3]
>> >>> list[10:0:-2]
>> [9, 7, 5, 3]
>
> For the specific list you show, the 2 expressions yield the same result.
> However this does not mean that this is true of any list.  The reason the 2
> expressions return the same result is because the starting index for the
> slice operation is effectively capped at 9, due to the list containing only
> 9 elements, so hence any value above 9 in the first slice parameter is
> essentially reset/capped to 9.

The start index is capped at 8:

    >>> slice(9, 0, -2).indices(9)
    (8, 0, -2)


More information about the Tutor mailing list