Python: Map VS For Loop - GeeksforGeeks

05 Aug, 2021
Map in Python :
Map is used to compute a function for different values ‘in a single line of code ‘ .
It takes two arguments, first is function name, that is defined already and the other is list, tuple or any other iterables .
It is a way of applying same function for multiple numbers .
It generates a map object at a particular location .
It works fast when we call an already defined function on the elements
 map(functionname, iterable)
 map(functionname, iterable)
Note: For more information refer to Python map() function.
for loop in Python :
We use for loop to repeat a block of code for fixed number of times .
Used when no results are required .
To perform sequential traversal .
Loop from 0 to n runs n+1 times .
 for var in iterable :
               statements Note: Here, var is the name given to iterating variable, iterable can be replaced by range() function and they can be of any data type . Statements are the step of actions to be performed .
 for var in iterable :
               statements 
Note: Here, var is the name given to iterating variable, iterable can be replaced by range() function and they can be of any data type . Statements are the step of actions to be performed .
Note: For more information, refer to Python For Loops.
Example:
Python
# function to square a given numberdef squareNum (a) :    return a * a    listt = [0, -1, 3, 4.5, 99, .08]  # using 'map' to call the function# 'squareNum' for all the elements# of 'listt'x = map(squareNum, listt)  # map function returns a map# object at this particular # locationprint(x)   # convert map to listprint(list(x))     # alternate way to square all# elements of 'listt' using# 'for loop'  for i in listt :    square = i * i    print(square)
Output:
<map object at 0x7fe413cf9b00>
[0, 1, 9, 20.25, 9801, 0.0064]
0
1
9
20.25
9801
0.0064

Comparing performance , map() wins! map() works way faster than for loop. Considering the same code above when run in this ide.Using map():using for loop:for loop can be with no content, no such concept exist in map() function.Example:PythonPython# we use the keyword 'pass'# to simply get a for loop # with no contentfor i in range (10) :    passThere can be an else condition in for loop which only runs when no break statement is used. There is nothing like this in map.Example :PythonPython# for loop with else condition  for i in range(10) :    print(i)else :     print("Finished !")Output :0
1
2
3
4
5
6
7
8
9
Finished !
for loop can exit before too. We can do that using break statement. Exiting before expected is not possible in map.map generates a map object, for loop does not return anything.syntax of map and for loop are completely different.for loop is for executing the same block of code for a fixed number of times, the map also does that but in a single line of code.
Comparing performance , map() wins! map() works way faster than for loop. Considering the same code above when run in this ide.Using map():using for loop:
Using map():
using for loop:
for loop can be with no content, no such concept exist in map() function.Example:PythonPython# we use the keyword 'pass'# to simply get a for loop # with no contentfor i in range (10) :    pass
Example:
Python
# we use the keyword 'pass'# to simply get a for loop # with no contentfor i in range (10) :    pass
There can be an else condition in for loop which only runs when no break statement is used. There is nothing like this in map.Example :PythonPython# for loop with else condition  for i in range(10) :    print(i)else :     print("Finished !")Output :0
1
2
3
4
5
6
7
8
9
Finished !

Example :
Python
# for loop with else condition  for i in range(10) :    print(i)else :     print("Finished !")
Output :
0
1
2
3
4
5
6
7
8
9
Finished !

for loop can exit before too. We can do that using break statement. Exiting before expected is not possible in map.
map generates a map object, for loop does not return anything.
syntax of map and for loop are completely different.
for loop is for executing the same block of code for a fixed number of times, the map also does that but in a single line of code.
akshaysingh98088
python-utility
Technical Scripter 2019
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | Get unique values from a list
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python | Pandas dataframe.groupby()