list.index crashes when the element is not found

TkNeo tarun.kap at gmail.com
Fri May 2 16:26:58 EDT 2008


On May 2, 2:49 pm, Jeff <jeffo... at gmail.com> wrote:
> The generally used idiom for that is:
>
> lst = ['a', 'b', 'c']
> if 'a' in lst:
>   foo = lst.index('a')

Jeff - Gracias !!

I am fairly new to python. Thanks for the example code snippet above.
It is the same amount of code as receiving -1 and then checking for
doing an "if else" for -1 so now i don't feel bad. But, being new to
this paradigm, raising of an exception when it can't find the element
appears to be weird to me for some unexplainable reason.



More information about the Python-list mailing list