yet another list compr. suggestion
Christoph Zwerschke
cito at online.de
Sun Jan 22 18:25:30 EST 2006
palo wrote:
> ... what would you think about this:
> as a substitute for filter, people often use
> [x for x in y if z(x)]
> The suggestion is that the same result could be achieved by
> [x in y if z(x)]
In the original syntax, if z(x) is always true,
you can leave the "if z(x)" part away, which is intuitive.
But in your syntax, the "if z(x)" is essential.
If you leave it away, you get a completely different
construct, a list with a bool value as element.
You would not expect that an "if" keyword indicates a loop.
I think the "for" is absolutely essential to recognize
a list comprehension, not only for the parser, but also
for the human reader. It's already confusing enough.
Just compare the following:
[x in y] -> list with one element
[x in y if z(x)] -> list comprehension (in your syntax)
[x in y if z(x) else 0] -> list with one element (Py 2.5)
-- Christoph
More information about the Python-list
mailing list