Python map()
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:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours