Exercise 13: map() function

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

Need More Exercises?

Check out Holy Python AI+ for amazing Python learning tools.

*Includes 14 more programming languages, inspirational tools and 1-on-1 consulting options.
Umut Sagir
Finance & Data Science Professional,
Founder of HolyPython