Python map

Python map


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]