a break for comprehensions

Tom Good Tom_Good1 at excite.com
Mon Jul 30 17:31:08 EDT 2001


"Steven D. Majewski" <sdm7g at Virginia.EDU> wrote in message news:<mailman.996515638.21022.python-list at python.org>...
> 
> The 'while' clause seems useful, and I don't see anything inherently
> ambiguous about it, but sticking a procedural clause into what's
> otherwise a declarative seens to go against the spirit -- what's 
> nice about list comprehensions is that they are just like set 
> notations. ( well -- the left hand side is often procedural -- it 
> would be even more readable if it were more clearly separated
> with a "where" clause, but that would be Yet Another Keyword... ) 
> 
> -- Steve Majewski

That is a compelling argument.  However, I am still struggling with
how to best work with infinite sets (which are now easy to create with
generators).  With set notation, you can refer to an infinite set
without actually creating each member of the set.  List comprehensions
try to "fill up" the list, which causes problems if an unbounded
generator is involved.

If g is a generator that generates all positive integers, I would like
to write something like:

L = [x * 2 for x in g()]
print L[:10]

This does not work, of course, but that is how I would tend to think
about it in my head.  List comprehensions with lazy evaluation?  Is it
possible?



Tom



More information about the Python-list mailing list