In this tutorial, you will learn:

Syntax:
How map() function works?
Using map() with Python built-in functions
Using map() with a string as an iterator
Using map() with listof Numbers
Using map() with Tuple
Using map() with Dictionary
Using map() with Set
Using map() with Lambda function
Using Multiple Iterators inside map() function

Syntax:

Parameters

Here are two important

function: A mandatory function to be given to map, that will be applied to all the items available in the iterator. iterator: An iterable compulsory object. It can be a list, a tuple, etc. You can pass multiple iterator objects to map() function.

Return Value

The map() function is going to apply the given function on all the items inside the iterator and return an iterable map object i.e a tuple, a list, etc.

How map() function works?

The map() function takes two inputs as a function and an iterable object. The function that is given to map() is a normal function, and it will iterate over all the values present in the iterable object given. For example, consider you have a list of numbers, and you want to find the square of each of the numbers. The get the output, we need the function that will return the square of the given number. The function will be as follows: The list of items that we want to find the square is as follows: Now let us use map() python built-in function to get the square of each of the items in my_list. The final code is as follows: Output: The output of the map() function, as seen in the output, is a map object displayed on the screen as <map object at 0x0000002C59601748>. You will have to iterate the output from the map using a for-loop or using list() method to get the final output. I have used list() in the code that displays the values inside the list given. So using map() function, we are able to get the square of each number.The list given to map was [2,3,4,5,6,7,8,9] and using the function square() the output from map() we got is [4, 9, 16, 25, 36, 49, 64, 81] . The function map() applies the function square() on all the items on the list. For example, my_list variable and updates the list with the square of each number. The out is stored in the updated_list variable.

Using map() with Python built-in functions

Python map() function is a built-in function and can also be used with other built-in functions available in Python. In the example, we are going to make use of Python round() built-in function that rounds the values given. Example: The list that i have is my_list = [2.6743,3.63526,4.2325,5.9687967,6.3265,7.6988,8.232,9.6907] . I need the rounded values for each item present in the list. We will make use of round() as the function to map(). Output: The round() function is applied to all the items in the list, and it returns back a list with all values rounded as shown in the output.

Using map() with a string as an iterator

We can also make use of map() on a string. In Python, a string acts like an array so we can easily use it inside the map(). In the example, we have a function myMapFunc() that takes care of converting the given string to uppercase. The function myMapFunc () is given to map() function.The map function will take care of converting the string given to uppercase by passing the string to myMapFunc(). Output:

Using map() with listof Numbers

To work with the list in map() will take a list of numbers and multiply each number in the list by 10. The list that we are going to use is : [2,3,4,5,6,7,8,9]. The function myMapFunc () takes care of multiply the given number with 10. The function is given to map along with the list. Example: Output: The output we see is that each number in the list is multiplied by 10.

Using map() with Tuple

A tuple is an object in Python that has items separated by commas and enclosed in round brackets. In the example, we will take a tuple with string values. The function that we will use will convert the values given to uppercase. Example: Output: The output we get is a tuple back with all the values in it are converted to uppercase.

Using map() with Dictionary

A dictionary in Python is created using curly brackets({}). Since the dictionary is an iterator, you can make use of it inside map() function. Let us now use a dictionary as an iterator inside map() function. Following example shows the working of dictionary iterator inside map() Output:

Using map() with Set

Set in Python is an unordered collection of items in curly brackets(()). Since set() is also an iterator, you can make use of it inside map() function. Here is a working example of using set as an iterator inside map() Output:

Using map() with Lambda function

In Python, lambda expressions are utilized to construct anonymous functions. You will have to use the lambda keyword just as you use def to define normal functions. So in the example, we are going to use the lambda function inside the map(). The lambda function will multiply each value in the list with 10. Example: Output:

Using Multiple Iterators inside map() function

Example 1: Passing two list iterators to map()

You can send more than one iterator i.e. a list, a tuple, etc. all at the same time to the map() function. For example, if you want to add two lists. The same can be done using the map() function. We are going to make use of two lists my_list1 and my_list2. In the example below, the first item in the my_list1 is added to the first item of my_list2. The function myMapFunc() takes in items of my_list1 and my_list2 and returns the sum of both. Here is the working example of adding two given lists using map() function. Output:

Example 2: Passing one Tuple and a list iterator to map()

We are going to make use of a list and a tuple iterator in map() function. The function is given to map – myMapFunc() will get the items from the list and Tuple. The items will be joined with an underscore(_). The working example is as shown below: Output:

Summary

Python map() is a built-in function that applies a function on all the items of an iterator given as input. An iterator, for example, can be a list, a tuple, a string, etc. and it returns an iterable map object. The map() function is going to apply the given function on all the items inside the iterator and return an iterable map object i.e a tuple, a list, etc. Python map() function is a built-in function and can also be used with other built-in functions available in Python. A tuple is an object in Python that has items separated by commas and enclosed in round brackets. In the example will take a tuple with string values. The function that we will use will convert the values given to uppercase. A dictionary in Python is created using curly brackets({}). Since the dictionary is an iterator, you can make use of it inside map() function. Set in Python is an unordered collection of items in curly brackets(()). Since set() is also an iterator, you can make use of it inside map() function. In Python, lambda expressions (or lambda forms) are utilized to construct anonymous functions. So the lambda keyword has to used when you want to use lambda inside the map(). You can send more than one iterator i.e. a list, a tuple to the map() function.