Map in Python :
For more information, refer to: Python map() function
for loop in Python :
For more information, refer to: Python For Loops
Example:
Python
# function to square a given number
def 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
# location
print(x)
# convert map to list
print(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
Map vs for loop
Comparing performance , map() wins! map() works way faster than for loop.
Using map():
Using for loop:
for loop can be with no content, no such concept exist in map() function.
Example:
Python
</p><pre><code class="language-python3 1==">
# we use the keyword 'pass'
# to simply get a for loop
# with no content
for i in range (10) :
pass
</code></pre><p></p><p dir="ltr"><span>There can be an </span><code><span>else</span></code><span> condition in </span><code><span>for</span></code><span> loop which only runs when no </span><code><span>break</span></code><span> statement is used. There is nothing like this in </span><code><span>map</span></code><span>.</span></p><p dir="ltr"><b><strong>Example : </strong></b><gfg-tabs data-run-ide="true" data-mode="light"><gfg-tab slot="tab">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.
Let us see the differences in a tabular form -:
| Map() | forloop |
1. | The map() function executes a specified function for each item in an iterable. | The for loop is used for iterating over a sequence. |
2. | Its syntax is -: map(function, iterables) | It is used by using for keyword. |
3. | In this, the item is sent to the function as a parameter. | It is used to execute a set of statements, once for each item in a list, tuple, set etc. |
4. | It takes two parameters function and iterables. | Its syntax is -: for var in iterable : statements |
Similar Reads
Map vs List comprehension - Python List comprehension and map() both transform iterables but differ in syntax and performance. List comprehension is concise as the logic is applied in one line while map() applies a function to each item and returns an iterator and offering better memory efficiency for large datasets.List comprehensio
2 min read
Python Lists VS Numpy Arrays Here, we will understand the difference between Python List and Python Numpy array. What is a Numpy array?NumPy is the fundamental package for scientific computing in Python. Numpy arrays facilitate advanced mathematical and other types of operations on large numbers of data. Typically, such operati
7 min read
Python map() function The map() function is used to apply a given function to every item of an iterable, such as a list or tuple, and returns a map object (which is an iterator). Let's start with a simple example of using map() to convert a list of strings into a list of integers.Pythons = ['1', '2', '3', '4'] res = map(
4 min read
Understanding for-loop in Python A Pythonic for-loop is very different from for-loops of other programming language. A for-loop in Python is used to loop over an iterator however in other languages, it is used to loop over a condition. In this article, we will take a deeper dive into Pythonic for-loop and witness the reason behind
6 min read
Python Foreach - How to Implement ? Foreach loop is a convenient way to iterate over elements in a collection, such as an array or list. Python, however, does not have a dedicated foreach loop like some other languages (e.g., PHP or JavaScript). We will explore different ways to implement a foreach-like loop in Python, along with exam
3 min read
Looping Techniques in Python Python supports various looping techniques by certain inbuilt functions, in various sequential containers. These methods are primarily very useful in competitive programming and also in various projects that require a specific technique with loops maintaining the overall structure of code. Â A lot of
6 min read