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


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.lower().count("a"), lst1))