Python map() Function

Example

Calculate the length of each word in the tuple:

def myfunc(n):
  return len(n)
x = map(myfunc, ('apple', 'banana', 'cherry'))

Run Instance

Definition and Usage

The map() function executes the specified function for each item in the iterable. The item is sent as a parameter to the function.

Syntax

map(function, iterables)

Parameter Value

Parameter Description
function Required. The function to be executed for each item.
iterable

Required. A sequence, set, or iterator object.

You can send any number of iterable objects, just make sure that each iterable has a parameter for the function.

You can send as many iterables as you like, just make sure the function has one parameter for each iterable.

More Examples

Example

Generate new fruits by sending two iterable objects to the function:

def myfunc(a, b):
  return a + b
x = map(myfunc, ('apple', 'banana', 'cherry'), ('orange', 'lemon', 'pineapple'))

Run Instance