Skip to content

Comment on Charming Python: Functional programming in Pythonparent

Comments

That depends. When you're doing simple translations the list way is much cleaner.

   ids = [item.id for item in all_items]
Seems much cleaner than
   ids = map(lambda item: item.id, all_items)
But yes, I agree, when you have to actually perform some less-than-trivial computation on every item of a list, map is much cleaner.

But the first of your examples is cleaner not because of any inherent advantage of list-comprehension syntax but because in Python the syntactic cost of turning an expression into an anonymous function is so high. In other words, you're comparing (cognitive cost of comprehension + low cost of expression) against (cognitive cost of map + high cost of promoting an expression to an anonymous function).

Don't blame map for Python's tax on expression-promotion. ;-)

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.