Python Zen - (only) one way to do it

1.6k views Asked by At

This question might be sound subjective, but as "the Zen" says, there is (nearly always) one way to preferred, it shouldn't be subjective at the end.

What way is the better one?

[i.something() for i in l]
map(operator.methodcaller('something'), l)
map(lambda x: x.something(), l)

(1) is (IMO) very clear, but in many answers, map() is used. And if we do so, there is nearly equal readability between (2) and (3) (IMO, at least).

The same counts for many other tasks, but I have chosen this one, as it can stand for all of similiar ones.

1

There are 1 answers

4
Tim Pietzcker On BEST ANSWER
  • Simple is better than complex.
  • Readability counts.

Both are clear arguments for [i.something() for i in l].

This assumes that .something() doesn't mutate i, and that you're on Python 2.