What's the difference between these 2 statements?

tiissa tiissa at nonfree.fr
Wed Apr 20 16:08:39 EDT 2005


ahmedt at gmail.com wrote:
> I'm sorry, I'm not really following your logic. Can you supply the
> statement with the three parameters ?
> 
> 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 ?

Contrary to what I said above x should be _strictly_ less than -len(s).
You stop when you reach in the list the given end index (and don't take 
the item there) or if you leave the index range.

But -1 as an index is the same as (len(s)-1).
Therefore going from len(s)-1 down to -1 is the same as going from 
len(s)-1 to len(s)-1 hence an empty list.

And -len(s) is the same as 0 (my mistake above)
But -len(s)-1 is not in the list thus you won't discard any limit.

The example:
In [1]: s='12345'

In [2]: s[len(s)-1:0:-1]
Out[2]: '5432'

In [3]: s[len(s)-1:-1:-1]
Out[3]: ''

In [4]: s[-1],s[len(s)-1]
Out[4]: ('5', '5')

In [5]: s[len(s)-1:-len(s)-1:-1]
Out[5]: '54321'

In [6]: s[len(s)-1:-len(s):-1]
Out[6]: '5432'



More information about the Python-list mailing list