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))