[Python-ideas] filter-only list comps

spir denis.spir at gmail.com
Fri Feb 26 09:43:06 CET 2010

On Fri, 26 Feb 2010 12:40:22 +1300
Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:

> spir wrote:
> > But the analog does not apply to filter-only list comps:
> > 	[x in numbers if x%2==1]	# SyntaxError
> If you were allowed to say that, you should also be
> able to say
>     [x in numbers]
> but this already has a meaning as an ordinary list
> constructor.

Yes, that's precisely the semantics it should have (consistent with my view that <item in collection> is the core of a comprehension), since there is neither a mapping, nore a filter.

> Requiring the word 'for' to appear in an LC ensures
> that there is never any ambiguity.

Yes, but the issue is rather with (x in numbers) beeing a test. (--> ambiguity of "in")

> However, it might be possible to phrase it another
> way, e.g.
>    [x from numbers if x%2==1]

Wow, I like that. Better term than "in", in my opinion. (and solves the ambiguity of "in", but then consistency require to change "for x in numbers", which has about -1% chances to happen, I guess ;-)



la vita e estrany


More information about the Python-ideas mailing list