[Python-ideas] list.index() extension

Benjamin Peterson benjamin at python.org
Sun Apr 5 02:39:24 CEST 2009



2009/4/4 Leif Walsh <leif.walsh at gmail.com>:
> Maybe you didn't understand what I meant.  Try the following code,
> with your favorite list and object.  I promise it works.
>
> try:
>  print "The index of the first element that 'is obj' is %d." % (idx
> for idx, elt in enumerate(lst) if elt is obj).next()
> except StopIteration:
>  print "obj is not in lst"

Yes, I saw that but it still strikes me as much uglier than necessary.



-- 
Regards,
Benjamin






More information about the Python-ideas mailing list