4 ms·
Python's list comprehensions always struck me as an odd way to go given Python's tendency to choose clarity and simplicity over complexity and expressiveness.
by kbenson 12y ago
Python's list comprehensions always struck me as an odd way to go given Python's tendency to choose clarity and simplicity over complexity and expressiveness. As a Perl user, Python list comprehensions make me go cross-eyed half the time. It's something Perl specifically decided to forego as too complex and prone to causing confusion, which is saying something.
- mpdehaan2 12y agoNesting them I feel is almost always bad blarg = [ foo(x) for x in y if x > 2] I think is pretty decent. Helps if they are calling named functions, so it's more self documenting, depending on complexity. Dict comprehensions can be a little brain-warping too :)
- kbenson 12y agoYeah, the Perl equivalent of that is: my @blarg = map { foo($_) } grep { $_ > 2 } @y; A bit more verbose, but if you know Perl, obvious (and the same building blocks allow for more powerful constructs[1], since they really are functional code blocks). You can argue the Python version is clearer, but placement of the conditional always throws me off. I like to think of my transforms as a pipeline. 1: http://en.wikipedia.org/wiki/Schwartzian_transform http://en.wikipedia.org/wiki/Schwartzian_transform