[Python-ideas] list.index() extension

Leif Walsh leif.walsh at gmail.com
Sun Apr 5 01:57:52 CEST 2009


On Sat, Apr 4, 2009 at 7:51 PM, Benjamin Peterson <benjamin at python.org> wrote:
> There's `next(itertools.ifilter(some_list, lambda x: x is my_obj))`, but that
> returns the object and not the index as I want.

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"

-- 
Cheers,
Leif



More information about the Python-ideas mailing list