by Riddhima Singh
601
The map()
π a Python built-in function returns an iterator after applying a given function to each item of an iterable (such as a list, tuple, etc).
Syntax: map ( function_name , iter )
Table of Contents
Example of map() function
test_list = [1, 2, 3, 4, 5]
# returns the cube of a number
def cube(n):
return n*n*n
# apply cube() to each item of the numbers list
cube_iterator = map(cube, test_list)
# converting to list
cube_number= list(cube_iterator)
print(cube_number)
Output
[1, 8, 27, 64, 125]
Return Value of map()
A map object is returned by the map()
function. You can pass the returned value to functions like
πlist() - To retrieves a list
πtuple() - To retrieves a tuple
πset() - To retrieves a set
πdictionary() is a function that retrieves a dictionary and many other functions
Implementation of map() on a list π―
#make function to use map with list
def map_list(l):
return (l+100)
List = [1,2,3,4,5,6,7,8,9,10]
result_list = map(map_list, List)
print(result_list)
print(list(result_list))
Output
<map object at 0x7f7176d1b460>
[101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
Implementation of map() on a tuple π―
def map_tuple(t):
return t.capitalize()
Tuple = ('hello','welcome','to','codeasify')
result_tuple = map(map_tuple, Tuple)
print(result_tuple)
print(tuple(result_tuple))
Output
<map object at 0x00000220A3C6B5B0>
('Hello', 'Welcome', 'To', 'Codeasify')
Implementation of map() on a set π―
def map_set(s):
return s+100
Set = {1,2,3,4,5,1,2,3,6,3}
result_set = map(map_set, Set)
print(result_set)
print((result_set))
Output
<map object at 0x000001F96E86B640>
{101, 102, 103, 104, 105, 106}
Implementation of map() on a dictionary π―
def map_dict(n):
return n+100
Dict = {1:3,2:5,3:7}
result_dict = map(map_dict, Dict)
print(result_dict)
print(list(result_dict))
Output
<map object at 0x7fa6f3b1ea70>
[101, 102, 103]
How to use lambda functions with map()
Lambda functions can be easily used with map() functions since map() expects a function as a first parameter. Let's look at the given below example.
map_lambda = [1, 2, 3, 4]
result_lambda = map(lambda x: x+100, map_lambda)
print(result_lambda)
# converting map object to tuple
addition = tuple(result_lambda)
print(addition)
Output
<map object at 0x7f082fe4f460>
(101, 102, 103, 104)
How to use the Lambda function to pass multiple iterators in map()
In this example, corresponding items from two lists are added.
map_lambda_1 = [1,2,3,4,5]
map_lambda_2 = [5,6,7,8,9]
result = map(lambda num_1, num_2: num_1+num_2, map_lambda_1 , map_lambda_2)
print(list(result))
Output
[6, 8, 10, 12, 14]
How to use Strings as an iterator in the map() function
As string can also be iterated so we can use string as an iterable in the map function.
A function map_str() converts a string into capitals when it is passed to result_str().
Example
def map_str(s) :
return s.capitalize()
string = "welcome to codeasify family!"
result_str = map(map_str, string)
print(result_str)
for i in result_str:
print(i, end="")
Output
<map object at 0x0000016C5136B520>
WELCOME TO CODEASIFY FAMILY!
Advantages of using Python's built-in map() function
β An iterable can be processed using map() without requiring a for loop, making your code simpler and more concise.
β For large iterables, map() is more efficient than using a for loop because it applies a function to each element in parallel.
β An iterable can be mapped to any function, including lambda functions, which can be helpful when creating simple functions in the middle of a program.