map()

Anonymous contributor's avatar
Anonymous contributor
Published Mar 25, 2022
Contribute to Docs

The map() built-in function accepts a function and applies it to every item in an iterable. It outputs a map object.

Syntax

map(func_name, iterable)

The func_name is the function to be applied to the iterable and can be a lambda function or the name of any defined function. The iterable, such as a list, contains the items the function will act on.

In the examples below list() is applied to the map expression to return a new list.

Example

The following example uses map() to return the length of each string in the list some_strings:

some_strings = ['learn', 'how', 'to', 'code', 'online']
print(list(map(len, some_strings)))
# Output: [5, 3, 2, 4, 6]

Codebyte Example

The map() function can be used to transform all the list items to integers:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy