|
|
|
|
|
by tmoertel
5658 days ago
|
|
> Usually list comprehensions are a much cleaner way to write a map. But the first makes my intent clearer than the second: map(f, xs)
[f(x) for x in xs]
In Python, where creating anonymous functions is syntactically expensive, list-comprehension syntax may be cheaper in many cases, but it's not clearer. When I want to map or filter something, I want to _say_ that I'm mapping or filtering, not explain to Python how to map or filter that thing in terms of list-comprehension syntax. Nor do I want to burden the reader with interpreting that syntax to figure out that it's a mapping or filtering operation.I want my code to say what I mean, not just evaluate to what I mean. |
|
That is entirely a matter of opinion. I would say python programmers with no functional background would find the list comprehension clearer, because that is the idiomatic python way.
May would have no idea what map even does without looking it up on the net.