Map, Filter, Lambda, and List Comprehensions in Python — MSE 350 vFall 2009 documentation
map(), filter(), lambda, and list comprehensions provide compact, elegant, and efficient ways to encode a few common idioms in programming. We often encounter the following scanarios involving for-loops: One way to achieve the same goals as in the above examples is to use some of Python’s tools from functional programming: map(), filter(), and lambda(). The map() function applies a function to every member of an iterable and returns the result. Typically, one would use an anonymous inline function as defined by lambda, but it is possible to use any function. The first example above can also be accomplished using map() as follows: In the case of the second example, the len() function already exists, so we can use map() to apply it to every element of the list: In the first map example above, we created a function, called square, so that map would have a function to apply to the sequence. This is a common occurrence, so Python provides the ability to create a simple (no statements allowed internally) anonymous inline function using a so-called lambda form. Thus, an anonymous function that returns the square of its argument can be written as lambda x: x**2. This means, “Here is an anonymous (nameless) function that takes one arguement, called x, and returns the square of x. Since the lambda form actually evaluates to a function, we can also call it inline. (This is generally a silly thing to do, but we show it here to demonstrate that lambda forms are actually inline function objects.): Lambda forms can be used as the required function argument to the map() function. For example, the first example above can be written as In English, this means, “Apply a function that squares its argument (the lambda form) to every member of the list of integers from 0 to 9 (the range()), and store the result in a list called squares. The fifth and sixth examples above can also be achieved with the filter() built-in function. Filter takes a function returning True or False and applies it to a sequence, returning a list of only those members of the sequence for which the function returned True. Lambda forms can also be used with the filter function, in fact, they can be used anywhere a function is expected in Python. In the fifth example, the list of squares is filtered according to whether the given entries are greater than 5 and less than 50. A lambda form that returns True when this condition is met is lambda x: x >, 5 and x <, 50. Thus, we can reproduce the fifth example as follows: In English, this means, “Find every member of the squares list for which the member is greater than 5 and less than 50 (every member for which lambda x: x >, 5 and x <, 50 returns True), and store that in a new variable called special_squares. Similarly, the sixth example <,sixth-example-list-comprehension>, can be reproduced using filter as follows: All of the six original examples can be achieved using a consistent, clean, and elegant syntax called list comprehensions. This will step over every element of sequence, successively setting loop-variable equal to every element one at a time, and will then build up a list by evaluating expression-involving-loop-variable for each one. This eliminates the need to use lambda forms, and thus generally produces a much more readable code than using map() and a more compact code than using a for-loop. Note that complex expressions can be put in the slot for expression-involving-loop-variable. For example, here we build up a list of names, first letters, and lengths for each name in the list: Where [name, name, len(name)] occupies the expression-involving-loop-variable slot, so that the list comprehension creates a list of [name, name, len(name)] for every name in the names sequence. [ expression-involving-loop-variables for outer-loop-variable in outer-sequence for inner-loop-variable in inner-sequence ] The final form of list comprehension involves creating a list and filtering it similarly to using filter(). The filtering form of list comprehension takes the following form: This form is similar to the simple form of list comprehension, but it evaluates boolean-expression-involving-loop-variable for every item and keeps only those members for which the boolean expression is True. Thus we can use list comprehensions to rewrite example 5 as Note that the above is inefficient, however, since it has to calculate the square of x three separate times for each element in the loop. Thus, the following is an equivalent and more efficient approach: Finally, note that the foregoing can be written on a single line using a pair of list comprehensions as follows: Source.