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.
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.
map( expression, iterable)
It accepts two parameters as arguments the "expression maps "and "iterable".
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 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.
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
added_two = [3, 4, 5, 6]
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
[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)
# 0 # 1 # 8 # 27 # 64
Python List Comprehension is used for creating a list where each element is generated by applying a simple formula on the given list.
resultant_list = [ <variable_expression> for <variable> in <input_list> ]
resultant_list = [ <variable_expression> for <variable> in <input_list> if <condition> ]
It takes two argument including a list.
It always returns a list.
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)
[ -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
[ 0, 1, 8]
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
At least one is under-aged, and they all are not allowed
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))
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))
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 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.
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 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
[ 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)))
So, in python filter vs. list comprehension, list comprehension has better performance.
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.
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 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.
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.
Your email address will not be published. Required fields are marked *