alist = [foo(word) for word in words if word.startswith('a')]
alist = map(foo, filter(lambda word: word.startswith('a'), words))
Which reads better? alist = [foo(word) for word in words if word.startswith('a')]
alist = map(foo, filter(lambda word: word.startswith('a'), words))
Which reads better? begins_with_a = lambda x: x.startswith('a')
alist = map(foo, filter(begins_with_a, words)) ;; clojure
(let [begins-with-a #(.startsWith % "a")
foo #(do-some-stuff-with %)]
(-> words (filter begins-with-a) (map foo))))
# ruby
words.filter { |e| e.start_with?(?a) }.map { |e| foo(e) }
It doesn't really make sense for things like `len` and `map` to be global functions in object-oriented languages. alist = words.filter(lambda word: word.startswith('a')
.map(foo)
That being said, my Python is limited and I don't know it filter/map are available as methods of a list. At the end of the day, there are cases where list comprehensions are much cleaner/understandable... and cases where the reverse is true.They're not. Which is a shame in my opinion, because as you've written it you can clearly read the operations in the order they happen, ie. filter followed by map. Instead, you do have to do the second line of what blossoms wrote above.
And I don't think it's possible to write a list comprehension that reads in execution order, either :(
(as it is now, list comprehensions requiring various references to result of a function call evaluate the function each time it's used)