Let's consider an iterable such as a List in Python:
mylist = [1, 2, 3, 4]
Now, we want each element of this
mylist to be incremented by 2. Let's consider we already have a function to do this:
def add_num(num): return num+2
We can use a for loop to do this task but let's learn a new interesting method provided by Python that is mapping where we can achieve the same thing without using a
for loop. Here,
map() function can help us out. Let's see how.
What is map() in python?
map() is a built-in function in Python that takes two parameters. The first parameter is a function and the second parameter is an iterable. The
map() function executes the function that is passed as the first parameter, on each element of the iterable that is passed as the second parameter. The
map() function returns a map object which is an iterable too. Let's see the syntax of
Syntax of map()
The function that is to be applied on each item of the iterable
iterable like set, list, etc that is to be mapped. (Can be more than one)
Let's take the previous example:
mylist = [1, 2, 3, 4] def add_num(num): return num + 2 new_list = map(add_num, mylist) print(new_list) for item in new_list: print(item)
<map object at 0x00000154853B9460>
Let's go through the above program:
new_list = map(add_num, mylist) here, every element of
mylist is passed as a parameter to the
add_num() and the entire modified list is saved in variable
new_list as an iterable.
Also, we can see by the output of
print(new_list), its a map object which is an iterable, so we iterate on it and get the desired output.
Let's take an example of passing two iterables:
def add_tup(a, b): return a + b new_list = map(add_tup, ('salt ', 'tea '), ('sugar', 'coffee')) print(tuple(new_list))
('salt sugar', 'tea coffee')
In the above example, we are using two iterables that are Tuples and executing a concatenation function on them with the help of
add_tup() function. Here, instead of iterating on the map object
new_list, we are simply converting it into a Tuple and printing it.
Try some examples with list and other iterables like set, dictionary,etc and see the results.
map() with lamba functions
Let's see in brief what is a lambda function in Python. Lambda functions, also known as anonymous functions, are functions without a name. The syntax is:
lambda arguments: expression
Here, lambda is a keyword. It can have any number of arguments but only one expression. The expression is executed and the result is returned. Let's see an example of lambda function:
x = lambda a, b: a + b print(x(5, 10))
Now, let's see how we can use lambda function with
mylist = [1, 2, 3, 4] res = map(lambda x: x*x*x, mylist) print(res) print(list(res))
<map object at 0x0000018077A4CB50>
[1, 8, 27, 64]
Here, instead of passing a function that has been defined before, as the first parameter, we are using a lambda function as the first parameter which calculates the cube of each element of the
Let's see one more example of
map() with the lambda function:
list1 = [1, 2, 3, 4] list2 = [2, 2, 2, 2] res = map(lambda x, y: x * y, list1, list2) # 1*2, 2*2, 3*2, 4*2 print(list(res))
[2, 4, 6, 8]
list1is multiplied with
list2with the help of lambda function and