Python map

Python map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]
 
 
 
def square(n):
 
    return n ** 2
 
 
 
squares = list(map(square, nums))
 
print(squares) # [1, 4, 9, 16, 25, 36, 49, 64, 81]
 
 
 
# lambda
 
squares_lambda = list(map(lambda x: x**2, nums))
 
print(squares_lambda) # [1, 4, 9, 16, 25, 36, 49, 64, 81]