Home >>Python Built-in Functions >Python map() Function
Python map() function is used to executes a given function for each item in a iterable. Each item is sent to the function as a parameter.
Syntax:map(function, iterables)
Parameter | Description |
---|---|
function | This is a required parameter. It defines the function to execute for each item. |
iterable | This is a required parameter. It defines a sequence, collection or an iterator object. |
def myfunc(a):
return len(a)
x = map(myfunc, ('Jerry', 'Joker', 'Alpha', 'Devil'))
print(x)
print(list(x))
def myfunc(a, b):
return a + b
x = map(myfunc, (1, 2, 3, 4, 5), (100, 202, 121, 231, 111))
print(x)
print(list(x))