On May 2, 3:26 pm, TkNeo <[EMAIL PROTECTED]> wrote: > On May 2, 2:49 pm, Jeff <[EMAIL PROTECTED]> 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.
I don't know how Python looks up internally 'a' in the list in the statement "if 'a' in lst: ... ," but just from common sense, I think it would have to do the work of finding it twice, first in the statement "'a' in lst," and then in the statement "lst.index('a')." If you had a lot of elements in the list, and/or you were looping a lot of times to find a lot of things, it'd be better to do the try/except form. -- http://mail.python.org/mailman/listinfo/python-list