This article will detail out
map() function in python, its use and syntax with example. It will also cover how to use lambda functions with
map()function is used to modify the items of an iterable to create another iterable.
Iterable here means anything that can be iterated or loop over such as a set, list, string.
The value returned by
map()function is an object of class map which is also an iterable and hence it can be converted to a set, list, string.
map() function accepts two arguments:
1. A function that transforms some value and returns it. This value is then added to the iterable returned by
2. An iterable whose elements are transformed by
Suppose we have a list of numbers and we want to generate another list whose elements are square of this list. Python code using
map() function will be
# Function that performs square of its argument def square(num): return num **2 # list of numbers numbers = [1,2,3,4,5] # call map function m = map(square, numbers) # print the type of return value print(type(m)) # convert map to list squares = list(m) print('Original List:', numbers) print('List of squares:', squares)
map()function accepts a function as argument. This function accepts a value and returns its square.
Note that there is no parenthesis around the function name. This is because parenthesis means that we are passing its return value.
Second argument to
map()is a list, which is an iterable. Each element of the list is implicitly passed to the function whose return value is then added to the resultant iterable.
Notice the output where the type of return value of
map() is an object of class map. Objects of map class are iterables, hence it is passed to the list constructor to create a list out of it.
Return value from map can be directly converted to a list without assigning it to an intermediate variable as
squares = list(map(square, numbers))
Convert to upper case with map()
Below is an example to convert elements of a list in lower case to upper case using
First step would be to create a function which accepts a character and returns it after converting to upper case.
Second is to define a list with some letters in lower case.
map() with the function name and list of lower case characters as arguments and convert its return value to a list.
Python code follows
# Function to convert to upper case def convert_to_upper(letter): return letter.upper() # define a list message = ['p','y','t','h','o', 'n'] # invoke map upper = list(map(convert_to_upper, message)) print('Original List:', message) print('Upper case List:', upper)
Cloning list with map()
map() function can also be used to create a copy of list or clone it. Simply return the value from the argument function as it is without any modification.
# argument function def return_value(val): return val # define a list message = ['p','y','t','h','o', 'n'] # invoke map function copy = list(map(return_value, message)) # modify the first element copy = 'P' print('Original List:', message) print('Copy List:', copy)
Above example creates a list using
map() function. Note that the argument function does nothing with the value received, it simply returns it.
First element of the copy list is changed and both the lists are then printed. Here is the output.
Original list remains unchanged. This means that we are successful at cloning the list.
map() with lambda
A lambda in python is a function without a name or an anonymous function. Lambda functions are defined at the place where they need to be used.
A lambda is defined using the keyword
lambda with the below syntax
lambda <arguments>: <function body>
map() requires a function as its first argument. It is not required to create a separate function just for passing it as argument to
Instead create a lambda function and pass it directly. Thus, the earlier example which creates a list of squares can be modified to use a lambda function as
# list of numbers numbers = [1,2,3,4,5] # call map function m = map(lambda n: n ** 2, numbers) # convert map to list squares = list(m) print('Original List:', numbers) print('List of squares:', squares)
Note that we are not required to create a separate function to calculate the square of a number.
List of squares: [1, 4, 9, 16, 25]
Till now we only looked at
map()with argument function taking only one parameter. But we may also need to supply multiple parameters to it.
Remember that we need to supply equal number of iterables as the number of parameters expected by the function.
Below is an example that adds elements of two lists and assigns them to a third list.
# Function to add two numbers def add(num1, num2): return num1+num2 list1 = [1,2,3,4,5] list2 = [6,7,8,9,10] sum = list(map(add, list1, list2)) print('List one:', list1) print('List two:', list2) print('Sum of lists:', sum)
Note that in this example, the function takes two arguments and there are an equal number of lists.
map() iterates over all the iterables, takes a value from all of these and passes them to the function.
Above code written using a lambda function is given below.
list1 = [1,2,3,4,5] list2 = [6,7,8,9,10] sum = list(map(lambda x,y: x+y, list1, list2)) print('List one:', list1) print('List two:', list2) print('sum of lists:', sum)
Now there is no need to define a separate function to add two numbers as lambda function does this inline.