[Python-ideas] Adding an optional function argument to all() and any() builtins

Nick Coghlan ncoghlan at gmail.com
Mon Nov 22 17:19:55 CET 2010

On Tue, Nov 23, 2010 at 1:58 AM, Bruce Frederiksen <dangyogi at gmail.com> wrote:
> On Mon, Nov 22, 2010 at 6:32 AM, Tim Lesher <tlesher at gmail.com> wrote:
>> That could be the source of some frightening, hard-to-detect regressions:
>> if any([-1,0,1], test=is_even):
>>    # never taken, because 0 is returned instead of True
> Good point!  The only way out of this for what I was wanting that I can
> think of becomes:
> first_even = next(filter(is_even, [-1, 0, 1]), None)
> In which case, I already have all of the tools that I need!

Again, the generator expression version will be more readable for most folks:

  first_even = next((x for x in seq if is_even(x)), None)


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list