Map filter vs list comprehension
ExampleThe filter or map functions should often be replaced by list comprehensions. Guido Van Rossum describes this well in an open letter in 2005:
The following lines of code are considered "not pythonic" and will raise errors in many python linters. filter(lambda x: x % 2 == 0, range(10)) # even numbers < 10 map(lambda x: 2*x, range(10)) # multiply each number by two reduce(lambda x,y: x+y, range(10)) # sum of all elements in listTaking what we have learned from the previous quote, we can break down these filter and map expressions into their equivalent list comprehensions; also removing the lambda functions from each - making the code more readable in the process. Readability becomes even more apparent when dealing with chaining functions. Where due to readability, the results of one map or filter function should be passed as a result to the next; with simple cases, these can be replaced with a single list comprehension. Further, we can easily tell from the list comprehension what the outcome of our process is, where there is more cognitive load when reasoning about the chained Map & Filter process. # Map & Filter filtered = filter(lambda x: x % 2 == 0, range(10)) results = map(lambda x: 2*x, filtered) # List comprehension results = [2*x for x in range(10) if x % 2 == 0]Refactoring - Quick Reference
where F and P are functions which respectively transform input values and return a bool |