[Python-ideas] Make len() usable on a generator
Todd
toddrjen at gmail.com
Fri Oct 3 17:40:18 CEST 2014
On Oct 3, 2014 5:22 PM, "Thomas Chaumeny" <t.chaumeny at gmail.com> wrote:
>
> Yes, it has to exhaust the generator to find the length, but that also is
what list(generator) or sum(generator) have to do and yet they are allowed
constructions.
>
> Actually I don't think that calling len(generator) would be very useful
with a generator variable, but with an "anonymous" generator using a
comprehension like:
> valid_customers = len(customer for customer in customers if
customer.is_valid())
>
> that would be useful.
You could just do:
sum(customer.is_valid() for customer in customers)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141003/ab4bfd6c/attachment.html>
More information about the Python-ideas
mailing list