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
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours