list prob

Gonçalo Rodrigues op73418 at mail.telepac.pt
Tue May 21 19:35:36 EDT 2002


On Tue, 21 May 2002 23:48:41 +0200, "kemu" <kemusoft at hotmail.com> wrote:

>I want to search a list if I do list.index("word to search") and the word
>isn't in the list I get an error I don't want to get one from python but
>want my own error like word is not in the list how ?
>or is there an other way to search the list for strings and if he doesn't
>find one he doesn't print an error ?
>

wrap list.index in a try block, e.g:

>>> a = ['test', 'another test']
>>> a.index('yet another test')
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
ValueError: list.index(x): x not in list
>>> try:
... 	a.index('yet another test')
... except ValueError:
... 	print "see, no error!"
... 	
see, no error!
>>> 

All the best,
Gonçalo Rodrigues



More information about the Python-list mailing list