Python map() applies a function on all the items of an iterator given as input. An iterator, for example, can be a list, a tuple, a set, a dictionary, a string, and it returns an iterable map object. Python map() is a built-in function.

In this tutorial, you will learn:

Syntax:

map(function, iterator1,iterator2 ...iteratorN)

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:

def square(n):
	return n*n	

The list of items that we want to find the square is as follows:

my_list = [2,3,4,5,6,7,8,9]

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:

def square(n):
    return n*n
my_list = [2,3,4,5,6,7,8,9]
updated_list = map(square, my_list)
print(updated_list)
print(list(updated_list))

Output:

<map object at 0x0000002C59601748>
[4, 9, 16, 25, 36, 49, 64, 81]

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().

my_list = [2.6743,3.63526,4.2325,5.9687967,6.3265,7.6988,8.232,9.6907]
updated_list = map(round, my_list)
print(updated_list)
print(list(updated_list))

Output:

<map object at 0x000000E65F901748>
[3, 4, 4, 6, 6, 8, 8, 10]

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().

def myMapFunc(s):
    return s.upper()
my_str = "welcome to guru99 tutorials!"
updated_list = map(myMapFunc, my_str)
print(updated_list)
for i in updated_list:
    print(i, end="")

Output:

<map object at 0x000000DF2E711748>
WELCOME TO GURU99 TUTORIALS!

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:

def myMapFunc(n):
    return n*10

my_list = [2,3,4,5,6,7,8,9]

updated_list = map(myMapFunc, my_list)
print(updated_list)
print(list(updated_list))

Output:

<map object at 0x000000EE2C061898>
[20, 30, 40, 50, 60, 70, 80, 90]

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:

def myMapFunc(n):
    return n.upper()

my_tuple = ('php','java','python','c++','c')

updated_list = map(myMapFunc, my_tuple)
print(updated_list)
print(list(updated_list))

Output:

<map object at 0x0000009C3C3A16A0>
['PHP', 'JAVA', 'PYTHON', 'C++', 'C']

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()

def myMapFunc(n):
    return n*10
my_dict = {2,3,4,5,6,7,8,9}
finalitems = map(myMapFunc, my_dict)
print(finalitems)
print(list(finalitems))

Output:

<map object at 0x0000007EB451DEF0>
[20, 30, 40, 50, 60, 70, 80, 90]

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()

def myMapFunc(n):
    return n*10
my_set = {2,3,4,5,6,7,8,9}
finalitems = map(myMapFunc, my_set)
print(finalitems)
print(list(finalitems))

Output:

<map object at 0x000000AC8F05DEF0>
[20, 30, 40, 50, 60, 70, 80, 90]

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:

my_list = [2,3,4,5,6,7,8,9]
updated_list = map(lambda x: x * 10, my_list)
print(updated_list)
print(list(updated_list))

Output:

<map object at 0x000000BD18B11898>
[20, 30, 40, 50, 60, 70, 80, 90]

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.

def myMapFunc(list1, list2):
    return list1+list2

my_list1 = [2,3,4,5,6,7,8,9]
my_list2 = [4,8,12,16,20,24,28]

updated_list = map(myMapFunc, my_list1,my_list2)
print(updated_list)
print(list(updated_list))

Output:

<map object at 0x0000004D5F751860>
[6, 11, 16, 21, 26, 31, 36]

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:

def myMapFunc(list1, tuple1):
    return list1+"_"+tuple1

my_list = ['a','b', 'b', 'd', 'e']
my_tuple = ('PHP','Java','Python','C++','C')

updated_list = map(myMapFunc, my_list,my_tuple)
print(updated_list)
print(list(updated_list))

Output:

<map object at 0x00000059F37BB4E0>
['a_PHP', 'b_Java', 'b_Python', 'd_C++', 'e_C']

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.

 

YOU MIGHT LIKE: