3 ms·
I think I would probably write [bar(foo(z)) for z in stuff if foo(z)] as [bar(y) for y in (z for z in stuff if foo(z))] or even as [bar(y) for
by LyndsySimon 8y ago
I think I would probably write
[bar(foo(z)) for z in stuff if foo(z)]
as
[bar(y) for y in (z for z in stuff if foo(z))]
or even as
[bar(y) for y in filter(foo, stuff)]
... although, I get that `map`, `apply`, and `filter` aren't generally considered pythonic.
Overall, I think I agree with you - the new syntax in PEP572 might be handy, but it isn't necessary and I would say that the cognitive overhead of encountering yet another syntax doesn't justify the benefit, much less the technical overhead for the interpreter.
- eswoo 8y agoIt needs to be [bar(foo(y)) for y in (z for z in stuff if foo(z))] (etc.) though, since `bar` takes as input the output of `foo`. This leads to the objectionable duplicate calls to `foo`, hence the new assignment expressions. I like Dunnorandom's [bar(x) for x in map(foo, stuff) if x] best for a correct result using existing syntax, or [bar(x) for y in (foo(x) for x in stuff) if y] if you don't like `map`.