Python Map Exercises

Let’s check out some exercises that will help understand map()  better.

Exercise 13-a

Write a map function that adds plus 5 to each item in the list.


map() function takes two arguments, first the function, then comma and second the name of the list to be used for the mapping function.

lambda x: x+5 will take x as input and return x+5 as output.

lst2 = list(map(lambda x: x+5, lst1))

Exercise 13-b

Write a map function that returns the squares of the items in the list.


map() function takes two arguments, first the function, then comma and second the name of the list to be used for the mapping function.

lambda x: x**2 will take x as input and return x**2 as output.

lst2 = list(map(lambda x: x**2, lst1))

Exercise 13-c

Write a map function that adds "Hello, " in front of each item in the list.


map() function takes two arguments, first the function, then comma and second the name of the list to be used for the mapping function.

lambda x: “Hello, ” + x will take x as input and return “Hello, x” as output.

lst2 = list(map(lambda x: "Hello, " + x, lst1))

Exercise 13-d

Using map() function and len() function create a list that's consisted of lengths of each element in the first list.


map() function takes two arguments, first the function, then comma and second the name of the list to be used for the mapping function.

map(function, list)
We can use pre-defined len function in place of function parameter.

lst2 = list(map(len, lst1))

Exercise 13-e

Using map() function and lambda add each elements of two lists together. Use a lambda with two arguments.


map() function takes two arguments, first the function, then comma and second the name of the list to be used for the mapping function.

lambda x,y: x+y can help achieve the expected result

lst3 = list(map(lambda x,y: x+y, lst1, lst2))

Exercise 13-f

Using map() function and lambda and count() function create a list which consists of the number of occurence of letter: a.


map() function takes two arguments, first the function, then comma and second the name of the list to be used for the mapping function.

lambda x: x.count(“a”) can help achieve the expected result.

lst2 = list(map(lambda x: x.count("a"), lst1))

Exercise 13-g

Using map() function and lambda and count() function create a list consisted of the number of occurence of both letters: A and a.


Very similar to previous but you just need to add one thing. Maybe if everything was in lower case it would be easier to count?

lower is a string method that converts all characters in a string to lower case.

Simply use as:

string.lower()
lst2 = list(map(lambda x: x.lower().count("a"), lst1))

Exercise 13-h

Using map() function, first return a new list with absolute values of existing list. Then for ans_1, find the total sum of the new list's elements.


map() function takes two arguments, first the function, then comma and second the name of the list to be used for the mapping function.

abs function can be used to calculate absolute values of integers and floats. It can also be iterated through lists using map function.

A quick way to calculate sum of iterables (such as lists, tuples and dictionaries in Python) is the sum function.

Simply use it as:

sum(list)

Finally, don’t forget, map function similar to filter function returns a map object that’s not very readable for humans although it works great for computers. To convert this weird object to something presentable we use list function which creates a proper Python list from another iterable object (in this case a map object).

Simply use it as:

list(map_object)
new_lst = list(map(abs, lst))
ans_1 = sum(new_lst)