[Python-ideas] FW: Map-then-filter in comprehensions

Chris Angelico rosuav at gmail.com
Tue Mar 8 14:01:42 EST 2016


On Wed, Mar 9, 2016 at 5:57 AM, Joseph Jevnik <joejev at gmail.com> wrote:
> If we were to translate this to python syntax we could have something like:
> [r + 1 for n in range(1, 11) for n * 3 as r if r % 4 == 0]
> There is no reason that the name binding needs to be a part of the predicate
> expression, they can just be seperate clauses. I think the `for expr as
> name` is nice because it matches the order that comprehensions over multiple
> iterators are evaluated like: `[n for n in ns for m in ms]`.

That's somewhat more appealing. Not enthused about "for expr as name";
maybe "with expr as name"? Aside from not calling __enter__ and
__exit__, it's the same kind of operation that a with block does. But
the semantic difference isn't a good thing.

ChrisA


More information about the Python-ideas mailing list