[Python-ideas] Function to return first(or last) true value from list

Tim Peters tim.peters at gmail.com
Tue Feb 18 23:42:21 CET 2014


[Ryan Gonzalez <rymg19 at gmail.com>]
> It isn't uncommon to try and get either the first or the last True value
> from a list. In Python 2, you'd do this:
>
> next((x for x in mylist if x))
>
> And, in Python 3, thanks to filter returning an iterator, you'd do this:
>
> next(filter(bool,mylist))
> ...

See here:

    http://bugs.python.org/issue18652

Never heard anyone ask for "last" before   "first" has been available
in a PyPI package for over a year.  Detail in the bug report.


More information about the Python-ideas mailing list