Idea for key parameter in all() builting, would it be feasible?

Chris Angelico rosuav at gmail.com
Wed Jun 19 12:20:29 EDT 2013


On Thu, Jun 20, 2013 at 2:14 AM,  <russ.pobox at gmail.com> wrote:
> And the following, although the same thing really as all(xrange(10**9)), is not as instant and will take even longer than the above.
>
>>>> all(map(lambda x: bool(x), xrange(10**9)))
>
> However if all by some chance (I don't know how this stuff works underneath) has a key parameter then we could do something like.
>
>>>> all(xrange(10**9), key=lambda x: bool(x))
>
> Which would return False instantly (ideally).

All you need is the iterator version of map(). In Python 3, that's the
normal map(); in Python 2, use this:

>>> from itertools import imap
>>> all(imap(lambda x: bool(x), xrange(10**9)))
False

It's roughly instant, like you would expect.

ChrisA



More information about the Python-list mailing list