5 ms·
I guess you're right, list/generator comprehensions are the idiomatic way to filter and map in python, with the caveat of needing to have it all in a single exp
by bilkow 4y ago
I guess you're right, list/generator comprehensions are the idiomatic way to filter and map in python, with the caveat of needing to have it all in a single expression (the same goes for lambda, actually).
I still feel like chained methods are easier to read/understand, but list comprehensions aren't that bad.
- dralley 4y agoEven in Rust I don't like chains that go beyond ~4 operations. At some point it becomes clearer when expressed as a loop.
- Iwan-Zotow 4y ago> with the caveat of needing to have it all in a single expression (the same goes for lambda, actually). one could use multiple expressions in lambda in (modern) Python
- vgel 4y agoDo you mean using the walrus operator? Because unless I missed a recent PEP, I don't know of a way to do this without something hacky like that.
- Iwan-Zotow 4y agoyes x = 1 y = 2 q = list(map(lambda t: ( tx := tx, ty := ty, tx+ty )[-1], [1, 2, 3])) print(q)