Python List Comprehensions vs Map/Filter
Comments debate the merits of Python list comprehensions versus map, filter, and reduce functions, focusing on readability, pythonic style, and functional versus imperative programming preferences.
Activity Over Time
Top Contributors
Keywords
Sample Comments
What's the rationale for preferring map/filter over list comprehensions?
Use list comprehensions instead of map or filter. They're much easier to read.
Are lists comprehensions (the proposed alternative) less "functional" than map/reduce/filter?
What is the benefit of list comprehensions over the trifecta of map, reduce, and filter?
List Comprehension is just syntax for functional coding (filter, map) but a very good one!
Depends on how you want to program: imperative vs functional.Personally I think list comprehensions are the most beautiful part of Python, though sometimes I use map() when I'm trying to be explicitly functional (I realize it's allegedly slower, etc).Generally I think list comprehensions are cleaner and allow you to write purer functions with fewer mutable variables. I disagree that deeply nested for loops are necessarily more readable.
Python list comprehensions _are_ map/reduce.
List comprehensions are "[x for x in list]". Map, reduce and filter are just functional constructs.
It is becoming (has become?) an established part of the culture -- "Lambda in a `map`? Use a list comprehension instead."Which is annoying, because there are times when using map or filter is cleaner; for example, if you're doing pipeline-y stuff, using the function-based approach is much easier to read and reason about than doing a bunch of nested comprehensions.I also tend to think people don't use itertools enough. But it catches people by surprise enou
It's a matter of comfort.I was doing imperative programming for years, and more functional now.You've got for loops, list comprehensions, newer dict comprehensions, map/filter, generators, etc. in Python.Python programmers tend to prefer for loops and comprehensions. List comprehensions, especially, dict comprehensions being much newer.Living mostly in the pre-Java 8 Java world, I used for loops as much as the next guy, like the rest of the Java world. But I really l