In this tutorial, we will learn about the python map() method and its uses with examples.
What is the python map() method?
The map() method will return a map object of each item in an iterable. For example, (list, tuple, etc.)
These items are sent to the map method as a parameter.
The syntax of map() method is:
map() parameters
The map() method takes two parameters as an argument.
- function – each item of the iterable will be passed to this function.
- iterable – A sequence or iterable object which is to be mapped.
Let’s see some examples of the map() method in python.
Example 1: Working of the Python map() Method?
The output will be as follows.
Since map() expects a method to be passed in, lambda methods are commonly used while working with map() methods.
A lambda method is a short method without a name. Visit this page to learn more about Python lambda methods.
Example 2: How to use the lambda method with map()?
The output will be as follows.
Rules of map() method
- The map() method applies a given method to each item of an iterable and returns a list of the results.
- The returned value from the map() (map object) can then be passed to methods like list() (to create a list), set() (to create a set), and so on.