[Python-ideas] list.index() extension

Benjamin Peterson benjamin at python.org
Sun Apr 5 00:00:13 CEST 2009


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:
>> I can also imagine this might be useful:
>>
>> 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.

>
> Okay, it's sort of ugly and rubyish, but I think it solves your case
> sufficiently that we don't need to change index().  If you can come up
> with a more pressing reason though, I'm all ears (and fingers,
> evidently).

Did you see the 2to3 one?




More information about the Python-ideas mailing list