Thanks to visit codestin.com Credit goes to holypython.com
Let’s check out some exercises that will help understand map() better.
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))
Write a map function that returns the squares of the items in the list.
lambda x: x**2 will take x as input and return x**2 as output.
lst2 = list(map(lambda x: x**2, lst1))
Write a map function that adds "Hello, " in front of each item in the list.
lambda x: “Hello, ” + x will take x as input and return “Hello, x” as output.
lst2 = list(map(lambda x: "Hello, " + x, lst1))