Suppose we have a function and we want to compute this function for different values in a single line of code . This is where map() function plays its role ! map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.)
Syntax: map(funcname, iterables)
Parameters:
funcname: It is the name of the function which is already defined and is to be executed for each item.
iterables: It can be list, tuples or any other iterable object.Return Type: Returns a map object after applying the given function to each item of a given iterable (list, tuple etc.)
Example:
# function to double the number def num (n) : return n * 2 lst = [2, 44, 5.5, 6, -7] # suppose we want to call function # 'num' for each element of lst, # we use map # creates a map object x = map(num, lst) print(x) # returns list print(list(x)) |
Output:
<map object at 0x7f859f3f05c0> [4, 88, 11.0, 12, -14]
Note: For more information, refer to Python map() function.
List Comprehension is a substitute for the lambda function, map(), filter() and reduce(). It follows the form of the mathematical set-builder notation. It provide a concise way to create lists.
Syntax:
[ expression for item in list if conditional ]
Parameters:
- Expression – based on the variable used for each element
- for ..in – ‘for’ followed by the variable name to use, followed by ‘in’
- if – to filter
Example:
lst = [2, 44, 5.5, 6, -7] # to double the number # list comprehension x = [i * 2 for i in lst ] print(x) |
Output:
[4, 88, 11.0, 12, -14]
Note: For more information, refer to Python List Comprehension and Slicing.
Map VS List Comprehension
- List comprehension is more concise and easier to read as compared to map.
- List comprehension allows filtering. In map, we have no such facility. For example, to print all even numbers in range of 100, we can write
[n for n in range(100) if n%2 == 0]. There is no alternate for it in map - List comprehension are used when a list of results is required as map only returns a map object and does not return any list.
- List comprehension is faster than map when we need to evaluate expressions that are too long or complicated to express
- Map is faster in case of calling an already defined function (as no lambda is required).
- Without lambda
Let us take a simple operation to print number in a given range. We perform this operation using both map and list comprehension one by one.

From the above Example, we can conclude that map performs better than list comprehension when a function is defined already. - With lambda in map
Let us take operations where we require a lambda inside the map(). The first operation is to add 2 to every number in the given range. Second operation is to square every number in the given range.
From the above code, we can observe that map still works better than list comprehension.
Note: There are cases when list comprehension can perform better than map where expressions are too long and complex.
Recommended Posts:
- Python List Comprehension | Segregate 0's and 1's in an array list
- Python List Comprehension and Slicing
- Python | List comprehension vs * operator
- Count set bits using Python List comprehension
- Difference between List comprehension and Lambda in Python
- Python List Comprehension to find pair with given sum from two arrays
- Python List Comprehension | Three way partitioning of an array around a given range
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- Move all zeroes to end of array using List Comprehension in Python
- List comprehension and ord() in Python to remove all characters other than alphabets
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Python Dictionary Comprehension
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert mixed data types tuple list to string list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python Program to convert List of Integer to List of String
- Python | Add list elements with a multi-list based on index
- Python | Subtract two list elements if element in first list is greater
- Python | Filter list of strings based on the substring list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Comparing Execution Time
Note : We will be using an in-built python library ‘timeit‘.

