s.index(x[, i[, j]]) will change the s ?

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Thu Sep 10 02:45:48 EDT 2009


On Wed, 09 Sep 2009 22:00:41 -0700, s7v7nislands wrote:

> When a negative index is passed as the second or third parameter to the
> index() method, the list length is added, as for slice indices. I don't
> understand the mean.  the list length is added, why? if it changed, the
> original will change ?
...
> I want a example, maybe: use the a negative index is passed as the
> second or third parameter, and see the length changed.


Passing a negative parameter doesn't change the list. Why don't you try 
it for yourself and see?

>>> alist = 'a b c a b c a b'.split()
>>> alist
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b']
>>> alist.index('a')  # start at the beginning
0
>>> alist.index('a', 1)  # start 1 from the beginning
3
>>> alist.index('a', 5)  # start 5 from the beginning
6
>>> alist.index('a', 8-3)  # start 3 from the end
6
>>> alist.index('a', -3)  # start 3 from the end
6
>>> alist  # alist is unchanged
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b']




-- 
Steven



More information about the Python-list mailing list