Tuple index

Erik Max Francis max at alcyone.com
Sun Feb 20 20:24:49 EST 2005


Larry Bates wrote:

> Tuples don't have all the nice methods that lists have
> so convert it to a list.
> 
> tuple=('a','b','c','d')
> l=list(tuple)
> 
> now you can do:
> 
> list.index('c')
> 
> which returns 2
> 
> Remember index returns -1 when nothing is found.

No, that's .find in strings that returns -1.  .index in lists raises a 
ValueError:

 >>> [1, 2, 3].index(4)
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
ValueError: list.index(x): x not in list

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   The map is not the territory.
   -- Alfred Korzybski



More information about the Python-list mailing list