ts 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.