In this tutorial, we are going to discover and learn about Map function in python – map() function.
Getting Started
The map() function is a Built-in function in Python. It returns an iterator that applies function to every item of iterable. In a simpler way, map() function is used to apply a function taking as argument to the items of a sequence object i.e. parameter of the map() function.
The next line is about the syntax of map() function.
Syntax
The syntax of map() function in Python is shown below –
map(function, iterables) # Or, map(function, iterable, ...)
As we can see, python map() function takes two arguments. They are –
- function: This function performs action defined within it on each item of iterable(s).
- iterables: It contains iterable like set, list or tuple. It can be one or many iterables.
These two arguments are necessary and important when we want to work with map() function because we will get a TypeError if any of them is omitted. Following example will explain it clearly.
def square(x) : return x**2 print(map(square))
Output:
TypeError: map() must have at least two arguments.
In the above Python code, map() takes only one argument named square which is the function we will apply to the items of iterable. We got a TypeError as output because map() function must takes at least two arguments and in our case here only one argument is given. So, the missing parameter here is the iterables. The output will be same if the iterable is given as argument and the function is missed.
Parameters of map() Function
As it is shown in the syntax, the map() function in Python takes at least two arguments. Its first argument is the function we will apply to items present in an iterable and the second argument is the iterable.
Return Type of map() Function
In Python, the map() function returns a map object that contains the new items obtained after applying the function to the iterable. So, as return value we will get a map object which will store the modified version of items present in the iterable.
Examples of map() Function with Output
Till now, we have learnt about parameters and returns type of map function in python – map(). Now, we will see some examples on how to use map() function in our programming.
-
Example 1
def inc(x) : return x + 1 print(map(inc, [2, 3, 4, 5]))
Output :
<map object at 0x0000025F40CF0CD0>
In above code, we pass two arguments in map function. They are –
- First argument is inc function. In this method, we are incrementing value of each element by 1.
- Second argument is list that contains element 2, 3, 4 and 5.
In this Python code, we’re trying to increment items contained in [2, 3, 4, 5]. Clearly, it means that we will add number 1 to each item present in this iterable. Thus, as output we got a map object that contains the modified version of each item.
We can really see the modified version of each item by converting the map object into a set object as shown below.
Using Set with map() Function
Now, we will use map() function with Set in python.
def inc(x) : return x + 1 print(set(map(inc, [2, 3, 4, 5])))
Output:
{3, 4, 5, 6}
This output is a set object that store the modified version of items contained in the iterable. So, the first modified value is 3, it corresponds to the items 2 present in the iterable and so on. We must keep in the mind that the modified items can also be unordered because the output is a set object and sets are unordered.
Note : Apart from set object we can also convert map object into list object or tuple object by using respectively list() constructor and tuple() constructor. The outputs are shown below.
Using List with map() Function
def inc(x) : return x + 1 print(list(map(inc, [2, 3, 4, 5])))
Output:
[3, 4, 5, 6]
Return values of map() function is passed to list() contructor that are used to create list. Finally, values of list are printed using print method. Visit to learn more about list in python.
Using Tuple with map() Function
def inc(x) : return x + 1 print(tuple(map(inc, [2, 3, 4, 5])))
Output:
(3, 4, 5, 6)
The previous outputs are obtained by using respectively list() constructor and tuple() constructor. We must also know the items of these outputs are ordered because list and tuple object are ordered.
-
Example 2
def square(x) : return x**2 print(list(map(square, [2, 3, 4, 5])))
Output:
[4, 9, 16, 25]
Here, we are trying to get the square of each items present in the iterable. We must notice here that the map object returned is directly converted into list object. That’s the reason why we got this output.
-
Example 3
def addition(x, y) : return x + y print(tuple(map(addition, [2, 3, 4, 5], [6, 7, 8, 9])))
Output:
(8, 10, 12, 14)
As we said above map() function takes at least two arguments. It means that it can take more than two arguments. So, in this example map() function took three arguments. Clearly in this example map() function took one function named addition and two iterables. Our purpose here is to add item at the position 0 in the first iterable to the item at the position 0 in the second iterable and so on. Also we use the tuple() constructor here to convert map object into tuple object. That is the reason why we got (8, 10, 12, 14) as output.
-
Example 4
def multiplication(x) : return x * 2 print(set(map(multiplication, [2, 3, 4, 5])))
Output:
{8, 10, 4, 6}
Here, we have multiplied by 2 each item present in the iterable and the output is converted into set object. So this output is not ordered like the iterable stored in a list.
Note : map() function in Python takes user-defined functions as well as lambda function. In all the previous examples, we have used user-defined functions. Now, we will use the lambda function as shown in the following example.
-
Example 5 – Using Lambda with map()
value = map(lambda x : x - 1, [2, 3, 4, 5]) print(list(value))
Output:
[1, 2, 3, 4]
Through this Python code we are decreasing value of each item contained in the iterable [2, 3, 4, 5]. In a simpler way, we decrement by 1 each item present in the iterable. Here, we use the lambda function, the anonymous function in Python. The result is a map object converted into list object. That’s the reason why we got this output.
That’s end of our post on map function in python – map() with examples. Visit for more info
Learn about our other topics on python at – Python Tutorial
You must be logged in to post a comment.