What's the difference between these 2 statements?

tiissa tiissa at nonfree.fr
Wed Apr 20 16:13:04 EDT 2005


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'



More information about the Python-list mailing list