Python map() function With Examples

Python map() function is used to apply a function on all the elements of specified iterable and return map object. Python map object is an iterator, so we can iterate over its elements. We can also convert map object to sequence objects such as list, tuple etc. using their factory functions.

Python map() function

Python map() function syntax is:

We can pass multiple iterable arguments to map() function, in that case, the specified function must have that many arguments. The function will be applied to these iterable elements in parallel. With multiple iterable arguments, the map iterator stops when the shortest iterable is exhausted.

Python map() example

Let’s write a function to be used with map() function.

It’s a simple function that returns the upper case string representation of the input object.

I am also defining a utility function to print iterator elements. The function will print iterator elements with white space and will be reused in all the code snippets.

Let’s look at map() function example with different types of iterables.

Python map() with string


Python map() with tuple


Python map() with list


Converting map to list, tuple, set

Since map object is an iterator, we can pass it as an argument to the factory methods for creating a list, tuple, set etc.


Python map() with lambda

We can use lambda functions with map() if we don’t want to reuse it. This is useful when our function is small and we don’t want to define a new function.


Python map() multiple arguments

Let’s look at an example of using map() function with multiple iterable arguments.

Output: 5 12 21 32

Notice that our function has two arguments. The output map iterator is the result of applying this function to the two iterable elements in parallel. Let’s see what happens when the iterables are of different sizes.


So when the arguments are of different sizes, then the map function is applied to the elements until one of them is exhausted.

Python map() with function None

Let’s see what happens when we pass the function as None.


You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: