For enquiries call:

Phone

+1-469-442-0620

April flash sale-mobile

HomeBlogProgrammingPython Map Vs List Comprehension

Python Map Vs List Comprehension

Published
05th Sep, 2023
Views
view count loader
Read it in
10 Mins
In this article
    Python Map Vs List Comprehension

    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. Here is a comprehensive article on sys.argv command line arguments in Python for deeper understanding. 

    Python Map and Python list comprehension are features that work differently but have some similarities. Their performance varies with the parameters we are using. Let's discuss what these maps and list comprehension features are, how they work, and their performances. In addition, read about Self in Python

    Map function:-  

    The map function takes an Expression and an IterableThe 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". 

    • expression- It is the formula you need to calculate the result. 
    • Iterable- It is the iterable whose each element will be calculated on from the expression. 

    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 argumen 

    # 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 speed  

    Map 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 
    #         64 

    List Comprehension  

    Python 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 = [ <variable_expression> for <variable> in <input_list> ]  

                  or 

    resultant_list = [ <variable_expression> for <variable> in <input_list> if <condition> ]  

    Parameters  

    It takes two argument including a list. 

    • variable_expressionIt is the formula containing a variable and the formula is used on the variable. 
    • variable- It is a variable to access each element.  
    • input_list- It is the given list from where each element will be accessed as variable and applied on the formula.  
    • condition- After calculating the expression, if it matches the given Cindy, then only it will appear in the resultant_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 

    ExamplePrint "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 allowed 

    python reduce vs list comprehension 

    Reduce- 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 

    #importing functools module where reduce( ) is defined 
    import functools   
    num = [ 1 , 2, 5, 3, 9 ]  
    #summation of elements using functools( ) 
    print("The sum of all list elements is : ")  
    print(functools.reduce(lambda a,b : a+b, num))  

    #output: 

    The sum of all list elements is: 20  

    How to reduce function works?  

    Step 1: 1 + 2 = 3  
    Step 2: 3 + 5 = 8  
    Step 3: 8 + 3 = 11  
    Step 4: 11 + 9 = 20  

    Example 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 : 20  

    How 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 = 20  

    Python List comprehensionUsed 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 comprehension 

    In 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 list, whereas filter function returns an iterable object, which is then converted to 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 Comprehension  

    Assume 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.  

    • List comprehension has a simpler configuration than the map function.  
    • List comprehension can be used together with if condition as replacement of filter method. Map function has no such functionality. However, we can feed the map function output to the filter function.  
    • List comprehension returns a list, whereas the map function returns an object of Iterable.  
    • List comprehension execution is faster than that of map function when the formula expression is huge and complex. 
    • Map function is faster than list comprehension when the formula is already defined as a function earlier. So, that map function is used without lambda expression. 

    Top Cities Where Knowledgehut Conduct Python Certification Course Online

    Python Course in BangalorePython Course in ChennaiPython Course in Singapore
    Python Course in DelhiPython Course in DubaiPython Course in Indore
    Python Course in PunePython Course in BerlinPython Course in Trivandrum
    Python Course in NashikPython Course in MumbaiPython Certification in Melbourne
    Python Course in HyderabadPython Course in KolkataPython Course in Noida

    Comparing Execution Time  

    Now 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 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. 

    Profile

    Abhresh Sugandhi

    Author

    Abhresh is specialized as a corporate trainer, He has a decade of experience in technical training blended with virtual webinars and instructor-led session created courses, tutorials, and articles for organizations. He is also the founder of Nikasio.com, which offers multiple services in technical training, project consulting, content development, etc.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Programming Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon