What is map function in Python?

map function helps to pass function as parameter along with iterable (like list, tuple, set, etc.,) so that within a single statement function can be called required number of times. Below is a simple program to explain map function usage.

numbers = [1,2,3,4,5,6,7]
def square(num):
    return num * num
numbers_sq = map(square,numbers)
print(list(numbers_sq)).

In the above map function is used which has square function passed along with the list. The square function shall be called as per the elements count in the list. Finally, we are assigning to numbers_sq and printing the list

Post a Comment

0 Comments