1# Python program to demonstrate working
2# of map.
3
4# Return double of n
5def addition(n):
6 return n + n
7
8# We double all numbers using map()
9numbers = (1, 2, 3, 4)
10result = map(addition, numbers)
11print(list(result))
1# Let's define general python function
2>>> def doubleOrNothing(num):
3... return num * 2
4
5# now use Map on it.
6>>> map(doubleOrNothing, [1,2,3,4,5])
7<map object at 0x7ff5b2bc7d00>
8
9# apply built-in list method on Map Object
10>>> list(map(doubleOrNothing, [1,2,3,4,5])
11[2, 4, 6, 8, 10]
12
13# using lambda function
14>>> list(map(lambda x: x*2, [1,2,3,4,5]))
15[2, 4, 6, 8, 10]
1# Python program to demonstrate working
2# of map.
3
4# Return double of n
5def addition(n):
6 return n + n
7
8# We double all numbers using map()
9numbers = (1, 2, 3, 4)
10result = map(addition, numbers)
11print(list(result))
12# result [2, 4, 6, 8]
1>>> numbers = [1, 2, 3, 4, 5]
2>>> squared = []
3
4>>> for num in numbers:
5... squared.append(num ** 2)
6...
7
8>>> squared
9[1, 4, 9, 16, 25]
10