[Python-ideas] list.index() extension

Leif Walsh leif.walsh at gmail.com
Sun Apr 5 00:07:37 CEST 2009


On Sat, Apr 4, 2009 at 5:58 PM, Benjamin Peterson <benjamin at python.org> wrote:
> 2009/4/4 Leif Walsh <leif.walsh at gmail.com>:
>> On Sat, Apr 4, 2009 at 5:38 PM, Benjamin Peterson <benjamin at python.org> wrote:
>>> print "The first number less than 5 in this list is %s" % (my_list.index(5,
>>> operator.lt),)
>>
>> print "The first number less than 5 in this list is my_list[%d]=%s" %
>> ((idx, elt) for idx, elt in enumerate(my_list) if elt < 5).next()
>
> That does something different. My would tell you the first index of a
> number less than 5 and your what tell you what that was.

Mine does both, actually, and you can get whichever part you need out of it.

> Did you see the 2to3 one?

Yes.  I don't know the details of the implementations of those
classes, but I think you could easily cook up something quite similar
to what I did above.

I'm not really -1 or +1 on this, I just think it's probably easier for
you to use a generator expression than to try to convince python-ideas
that this needs to happen.  My finger could be way off the list's
pulse though.

-- 
Cheers,
Leif



More information about the Python-ideas mailing list