What's the difference between these 2 statements?
Peter Otten
__peter__ at web.de
Wed Apr 20 16:32:04 EDT 2005
tiissa wrote:
> Peter Otten wrote:
>> ahmedt at gmail.com wrote:
>>
>>
>>>so if I want to reverse it fully using s[len(s)-1:x:-1] what would x be
>>>or is it impossible to express it in this way ?
>>
>>
>> This does not work for integers, because the theoretically correct value
>> x = -1 already has another interpretation as the gap between the last and
>> the last but one character.
> AFAIK, it is not an issue of integer (what else can an slice index be in
> python?) but simply of index aliasing.
>
> For x=-len(s)-1, you get the whole reversed list:
>
> In [5]: s[len(s)-1:-len(s)-1:-1]
> Out[5]: '54321'
Clever. I didn't think of that.
Still, for practical purposes you have to test for slicelen >= stringlen, so
whether you choose None, -len(s)-1, or -sys.maxint as the second slice
parameter doesn't matter much.
Peter
More information about the Python-list
mailing list