[Python-ideas] filter-only list comps
denis.spir at gmail.com
Fri Feb 26 10:04:59 CET 2010
On Fri, 26 Feb 2010 13:11:10 +1300
Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Fred Drake wrote:
> > I like this, but it has the same readability issue I noted for cases
> > where decomposition is used:
> > [(k, v) from somedict.items() if condition(v)]
> I don't think that's too bad if you keep in mind that
> [x from stuff]
> is a shorthand for
> [x for x in stuff]
> whatever x happens to be. So your example would be
> equivalent to
> [(k, v) for (k, v) in somedict.items() if condition(v)]
> As a bonus, the short form could probably be made more
> efficient, because the tuples produced by the for-loop
> could be put straight into the result list, instead of
> having to re-pack k and v into a new tuple.
(Discussing my own shorthand proposal :-)
Sure, but remains a lexical issue:
[x in stuff] (x in stuff)
cannot be accepted because of ambiguity with membership test in the iterator case.
[x from stuff] (x from stuff))
solves that. But then for consistency "from" should be extented to all comprehensions
[x*x for x from stuff if x%2==1]
and, more importantly, to traversal loops:
for x from stuff:
because the sense is analog.
"in" would remain only as membership test operator.
This is very few probable ;-)
The issue is the ambiguity of "in".
la vita e estrany
More information about the Python-ideas