Python list comprehension vs map filter
Is there a reason to prefer using map() over list comprehension or vice versa? Is either of them generally more efficient or considered generally more pythonic than the other? Show when would using list comprehensions be better than using map() and lambda? Also whats ‘better’ and more easy to follow? Get full access to Python Cookbook and 60K+ other titles, with free 10-day trial of O'Reilly. There's also live online events, interactive content, certification prep materials, and more.
Credit: Luther Blissett You want to perform an operation on all the elements of a list, but you’d like to avoid using map and filter because they can be hard to read and understand, particularly when they need lambda. Say you want to create a new list by adding 23 to each item of some other list. In Python 1.5.2, the solution is: thenewlist = map(lambda x: x + 23, theoldlist)This is hardly the clearest code. Fortunately, since Python 2.0, we can use a list comprehension instead: thenewlist = [x + 23 for x in theoldlist]This is much clearer and more elegant. Similarly, say you want the new list to comprise all items in the other list that are larger than 5. In Python 1.5.2, the solution is: thenewlist = filter(lambda x: x > 5, theoldlist)But in modern Python, we can use the following list comprehension: thenewlist = [x for x in theoldlist if x > 5]Now say you want to combine both list operations. In Python 1.5.2, the solution is quite complex: thenewlist = map(lambda x: x+23, filter(lambda x: x>5, theoldlist))A list comprehension affords far greater clarity, as we can both perform selection with the if clause and use some expression, such as adding 23, on the selected items: thenewlist = [x + 23 for x in theoldlist if x > 5]Elegance and clarity, within a generally pragmatic attitude, are Python’s core values. List comprehensions, added in Python 2.0, delightfully display how pragmatism can enhance both clarity and elegance. The built-in map and filter functions still have their uses, since they’re arguably of equal elegance and clarity as list comprehensions when the lambda construct is not necessary. In fact, when their first argument is another built-in function (i.e., when lambda is not involved and there is no need to write a function just for the purpose of using it within a map or filter), they can be even faster than list comprehensions. All in all, Python programs optimally written for 2.0 or later use far fewer map and filter calls than similar programs written for 1.5.2. Most of the map and filter calls (and quite a few explicit loops) are replaced with list comprehensions (which Python borrowed, after some prettying of the syntax, from Haskell, described at http://www.haskell.org). It’s not an issue of wanting to play with a shiny new toy (although that desire, too, has its place in a programmer’s heart)—the point is that the toy, when used well, is a wonderfully useful instrument, further enhancing your Python programs’ clarity, simplicity, and elegance. The Reference Manual section on list displays (the other name for list comprehensions). Get Python Cookbook now with O’Reilly online learning. O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.
Python has many inbuilt methods, data structures, and modules to get the desired output. When we write a huge code consisting of many lines for a detailed problem statement, it is difficult to debug the code. Python provides the features we need to write code with a fewer number of lines. Python Map and Python list comprehension are features that work differently but have some similarities. Their perfomance varies with the parameters we are using. Let's discuss what these maps and list comprehension features are, how they work, and their performances. Map function:-The map function takes an Expression and an Iterable. The output will be an Iterable object where the expression will work on each element of the given Iterable. The output of each expression will be an element of the resultant Iterable. Syntax map( expression, iterable) Parameters It accepts two parameters as arguments the "expression maps "and "iterable".
Return type It returns an object of the iterable where the given formula calculates each element (as of 1st argument) applied on the elements of given iterable (2nd argument) of the map function. The output can be converted to an iterable. Example Example 1: Map ( ) using the function as the first argument. #This code will add "2" to each element of the list def add_two(n): #parameter is "n" return n + 2 #add "2" to "n" #application of formula "add_two( )" on every element of "num" num = [1, 2, 3, 4] #2nd argument added_two = map(add_two, num) #returns "added_two" as object print(list(added_two)) #object "added_two" is converted to list.#Output: added_two = [3, 4, 5, 6]Example 2: Using Lambda expression as first argument # Double up all numbers through map and lambda num = [1, 2, 3, 4] #input list, 2nd argument added_two = map(lambda i: i + 2, num) #output list object print(list(result)) #object is converted to list Iterable#Output: added_two = [3, 4, 5, 6]map vs. for loop python speedMap and For Loop can produce the same output, but they work differently. Inside "for loop", we iterate a variable fed to an expression within an Iterable. Whereas "Map" is used to map a function to all the elements of the given Iterable. It is a one-lined code configuration of "for loop". So, if we give the same input and write code to get the same output using "map" and "for loop", we will see from the time of execution on the IDE that their speed of execution is different. Run the below codes in your Python IDE and you will see that "map" is faster than "for loop". So, in map vs for loop python speed , map wins. Example solution with "map" : #definition of function to cube a given number def numCube (n) : return n * n * n num = [0, 1, 2, 3, 4] cubedNum = map(cubedNum, num) #object of iterable print(list(cubedNum)) #object is converted to Iterable list#output: [0, 2, 8, 27, 64]Example solution with "for loop" : num = [0, 1, 2, 3, 4] #input list for n in num : cubedNum = n * n * n #expression print(cubedNum)#output: # 0 # 1 # 8 # 27 # 64List ComprehensionPython List Comprehension is used for creating a list where each element is generated by applying a simple formula on the given list. Syntax resultant_list = [ or resultant_list = [ Parameters It takes two argument including a list.
Return type It always returns a list. Example Example 1: Python List comprehension without condition # a list comprehension to print cube num= [-2, -1, 0, 1, 2] cubedNum = [n**3 for n in num] print(cubedNum)#output: [ -8, -1, 0, 1, 8]Example 2: Python List comprehension with condition # a list comprehension to print cube if cube is positive num= [-2, -1, 0, 1, 2] cubedNum = [n**3 for n in num if n**3 >= 0] print(cubedNum) #output list contains only positive cubes#output: [ 0, 1, 8]python any list comprehension (use of "any" keyword)The "any ( )" function is very useful tool which works on iterables, and returns true if any of the element in the iterable is true. As list compression generates a list, we can use any() function with a python list comprehension. Example: Print "not allowed" if any one of the ages is less than 18 ages= [22, 15, 20, 25, 34] if any([a < 18 for a in ages]): print("At least one is under-aged and they all are not allowed") # there is one age 15 which is less than 18#output:. At least one is under-aged, and they all are not allowedpython reduce vs list comprehensionReduce- Used to reduce the list where a formula is applied recursively on the elements of a list. It accepts two arguments, including expression as the first argument and Iterable as the second argument. This function can be defined in the "functools" module. Firstly it took the first two elements of the Iterable and applied them to the expression. The calculated result and next element are applied to the expression. It is traversed till the last element of the Iterable, and the final output is thus generated. Example 1: Reduce function using lambda #output: The sum of all list elements is: 20How to reduce function works? Step 1: 1 + 2 = 3 Step 2: 3 + 5 = 8 Step 3: 8 + 3 = 11 Step 4: 11 + 9 = 20Example 2: Reduce function using operator module #importing functools module where reduce( ) is defined import functools #importing operator module where numerical operators are defined as function import operator num = [ 1 , 2, 5, 3, 9 ] print ("The sum of the list elements is : ") # "add" function is used as '+' operator print (functools.reduce(operator.add, num))#output: The sum of all list elements is : 20How does reduce function with operator work? Step 1: 1 add 2 = 3 Step 2: 3 add 5 = 8 Step 3: 8 add 3 = 11 Step 4: 11 add 9 = 20Python List comprehension- Used to create another list where a formula is applied once on each element of the list. So, we cannot use reduce in place of list comprehension. We have seen no possible answer to python reduce vs list comprehension because these two can replace each other. Python filter vs list comprehensionIn python, a filter is a function that is used to filter out some elements from Iterable based on a certain condition. It returns an Iterable object. We can visualize that after printing it, and converting it to a list. It can be done by using list comprehension to create a list with the "if" condition. When we are using a list with a small number of elements, then the list comprehension and filter method have almost the same execution speed. But when the list size is huge, then list comprehension is slightly faster than a filter. This is because list comprehension directly generates a list, whereas filter function returns an iterable object, which is then converted to a list. However, if we don't convert the object to a list, then the execution time is almost negligible. But to visualize the output, we need a list, not an object. So, python list comprehension is better than a filter. Example 1: Using list comprehension to print positive numbers from a list. num= [-2, -1, 0, 1, 2] positive Num = [n for n in num if n > 0] print(positiveNum) #output list contains only positive cubes#output: [ 1, 2]Example 2: Using filter function to print positive numbers from a list. num= [-2, -1, 0, 1, 2] print(list(filter(lambda n: n>0, num)))# output: [1, 2]So, in python filter vs. list comprehension, list comprehension has better performance. Python Map VS List ComprehensionAssume that we have a formula. We want to apply that formula to each element of a list and thereby create a new list. Now, using for loop, we can traverse through that given list, pick each item, apply them into the formula and append it to the resultant list. That is a long process that consists of many lines of code. So, in one line, we can use the map function as discussed above. Now, python list comprehension is a tool for creating a new list. We can use the functionality of lambda expression, filter method, and map function together using only this list comprehension.
Comparing Execution TimeNow to examine the execution performance of list comprehension and map function, we will import a module "timeit" to check the execution time. Let us see the result with a variety of methods. Without lambda: Map is faster than List Comprehension when function is already defined in case of map function. Example code 1: This code will print the time taken to evaluate numbers from 1 to 50. Map function is used without lambda. import timeit # list comprehension l1 = timeit.timeit( '[ l for l in range(50)]' , number = 999999) print (l) #map function f= 'def num( ) : print (n)' m1 = timeit.timeit( ' map (num, range(50))' , number = 999999, setup = f ) print (m)With lambda in map: List comprehension is better than map function when we don't define the function beforehand and use lambda expression inside map. Example code 2: This code will print the time taken to add a number to itself and this is applied for each element of the list. The expression is defined as lambda expression. import timeit # list comprehension l2 = timeit.timeit( '[ n+n for n in range(50)]' , number = 999999) print (l) #map function m2 = timeit.timeit( ' map (lambda a: a+a, range(50))' , number = 999999, setup = f ) print (m)Write the above codes on your IDE. You will see that for the first case, m1 is very less than l1. That means the map works faster than list comprehension. For the second case, m2 is greater than l2, implying that list comprehension is faster than map function when map function is used with Lambda expression. Conclusion There are no clear answers about which is the better option, in Python Map Vs List comprehension. We should know what the problem statement is, then use the method which suits us better because our main objective is to calculate the solution with optimal time and space consumption. |