Map Function In Python

In this tutorial, I will discuss the map function in python. for a better understanding of concepts, I will also show you the four examples and exercises in the end. So let’s start with the definition.

Definition :

map() is the builtin function that accepts a function(or any caller) and iterators(list, tuple) as an argument and applies the function to each element of the iterable and returns the iterator object to the caller.

Syntax :

map(fun, iterable)

Map Function In Python Examples

For the better understanding of the concepts let’s look at some examples

Find Squares : Example 1

In the following example, we will use the map function in python to find the square of numbers.

Before diving into map function this is how will do without using map.

# Without Map Function
nums = [1,2,3,4]

def square(num):
    return num**2

nums_square = []
for i in nums:
    sq = square(i)
    nums_square.append(sq)

print(nums_square )

Now let’s do the same with map function.

nums = [1,2,3,4]

def square(num):
    return num**2

nums_sq_map = map(square,nums)
# Note : map function returns the Map iterator object. 
# we have to manually convert it to the list
nums_sq = list(nums_sq_map)
print(nums_sq)

Save and run the above code you should see the following output.

Output :

map function in python code example output.

Passing Multiple Iterators : Example 2

One important point to note is we can pass more than one iterables to the map function. but for that function must take that many arguments as iterables and is applied to the items from all iterables in parallel.

In the following example , we will add two lists using map function.

def adder(element1,element2):
    # Element 1 : element from iterable 1
    # Element 2 : element from iterable 2
    return element1 + element2

num1 = [1,2,3]
num2 = [10,20,30]

num3_map = map(adder,num1,num2)
num3 = list(num3_map)
print(num3)

Output :

[11, 22, 33]

For multiple iterables, if the number of elements of all iterables are not same then the iterator stops when the shortest iterable is exhausted.

Here is the example of it.

num1 = [1,2,3]
num2 = [10,20,30,40,50]

num3_map = map(lambda e1,e2:e1+e2 ,num1,num2)
num3 = list(num3_map)
print(num3)

Firstly, In the above example, we used lambda functions in place of normal function that’s because it not only reduces the number of lines of code but also improves the readability of the program.

Secondly, we passed two lists of different sizes to the map function. but the length of the output is equal to the length of the list of minimum elements.

Lower Case To Upper Case : Example 3

In this example, we will use the map function to convert elements of the list from lowercase to uppercase.

s_lower = ['rat','dog','cat']
s_map = map(lambda string: string.upper(),s_lower)
# This is also correct
#s_map = map(str.upper,s_lower)
s_upper = list(s_map)
print(s_upper)

Output :

['RAT', 'DOG', 'CAT']

Get Element Length : Example 4

In this example, we will use the map function to find the length of each element of the list. Here is what I mean.

e = ['apple','banana','orange']

l_map = map(len,e)
l = list(l_map)
print(l)

Output :

[5,6,6]

Exercise For Readers :

Before wrapping up this tutorial here is the fun exercise for readers. can you guess the output of below programs?

Program 1 :

nums = {1:"a",2:"b",3:"c"}

nums_sq_map = map(lambda n : n**2,nums)
nums_sq = list(nums_sq_map)
print(nums_sq)

Program 2 :

nums = {"a":1,"b":2,"c":3}

nums_sq_map = map(lambda n : n**2,nums)
nums_sq = list(nums_sq_map)
print(nums_sq)

That’s the wrap for the tutorial of map function in python.

Also read about filter function in python

Leave a Reply