[ x for x in xrange(10) when p(x) ]

Alex Martelli aleax at mail.comcast.net
Thu Nov 10 10:14:54 EST 2005


bonono at gmail.com <bonono at gmail.com> wrote:

> I use "list" in the name in "english"/general sense(say a list in
> haskell is lazily evaluated), it could be a list or it could be a
> lazily evaluated iterable.

OK, but the general point is:
    [x for x in <whatever>]  is best written list(<whatever>)
    (x for x in <whatever>)  is best written iter(<whatever>)
(and if <whatever> is already a list and doesn't need to be copied, or
respectively is already being iterated on, just <whatever>).


> The original post is really just about "when" or may be "until" syntax
> that makes it a bit shorter to read and hopefuly easier to understand.

This is the first time on this thread in which I'm glimpsing that you
mean 'when' not as in SQL (where it has just the same meaning as the
'if' in Python's genexps/listcomps), but rather with the meaning that
any Pythonista would instinctively spell 'while'.  Since AFAIK 'when' is
only used in SQL (out of widespread languages), using it with a
drastically different meaning would be an utter disaster, IMHO.

Right now, listcomps and genexps can be explained very simply as
equivalent to just the same nesting of for and if statement as they have
clauses in sequence.  Adding a 'while' clause (or 'until', etc) would
unfortunately break this simple rule, and therefore make the whole
construct harder, not easier, to understand.  Unless there are very
compelling and frequent use cases for such an addition, I doubt it's
worth even trying to make a patch in order to time it against
itertools.takewhile...


Alex



More information about the Python-list mailing list