Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

The map() Method in Python

The Map() Method In Python

Introduction

The map() method in Python is vastly used to apply a function or operation on a sequence of data. The method after applying a specific function to all the elements of iterable returns a map object.

This map object is iterable and can be converted to the desired form. So, let us learn how it works and how we can use it in our programs.


How the map() method works

In general, the syntax for the map() method is given below,

map_object= map( method , iterable_object, ... )

Here,

  • The method is the function to which the map() method is going to pass the individual elements from the iterable object,
  • iterable_object is the data set on which we want to perform the function operation. We can pass multiple iterables to the method map() by simply separating them with commas(‘,’). The function would be applied to all of them parallelly,
  • map_object is the iterable object returned by the map() method after the function has been applied to all the elements of the provided objects.

So now look at a basic example to understand the method,

def make_even(n):        #defining a function that checks if a number is even or not and makes it even if it was not
  if n%2==0:
      return n
  else:
      return n+1

num = (1, 2, 3, 4)
map_obj = map(make_even, num) #the map() method applies the function make_even to all the elements of the tuple num
print(type(map_obj))   #printing the type of the object returned by map()
print(list(map_obj))   #type-casting the map object to list and printing it

Output:

<class 'map'>
[2, 2, 4, 4]
  • In the above example, we have considered a tuple num having 4 elements 1,2,3, and 4,
  • Also, we have defined a function make_even() which returns the number itself if it is even, or else adds 1 to the number makes it even and then finally returns the value,
  • After we have used the map() method, passing the function make_even and iterable num as parameters, the resultant object returned is stored into map_obj,
  • Printing the type(map_obj) tells us about the type of object it is. Which turns out to be a member of the map class,
  • Again, we type-caste the map_obj into a list and print the same to check the results. Which as expected gives us the desired output.

Using the map() method in Python

1. Using the lambda function in the map()

As we know, a lambda function are restricted functions which are minimalistic in size and cannot be reused. Using a lambda function instead of defining a whole new function is useful if we do not need the function further in the code and reduce the number of the function definition (resulting in smaller code). Let us take an example to understand the use of a lambda function in a map(),

num = (1, 2, 3, 4)
sqrd_obj = map(lambda n: n*n, num) #the map() method applies the lambda function to all the elements of the tuple num
print(list(sqrd_obj))   #type-casting the map object to list for printing it

Output:

[1, 4, 9, 16]
  • So, in the code above, we define a temporary lambda function which takes a number and returns the squared term,
  • As we can see from the output, we get the exactly squared terms of the individual elements of the tuple num,
  • In this way, using the lambda function we reduced the code size and even didn’t had to define a separate function to do the task.

2. Using multiple arguments in the map()

As we have discussed earlier, we can actually pass multiple iterable objects to the function specified to the map() method. Let us take an example to understand the working of the same, parallelly

num1= (1, 2, 3, 4)  #tuple
num2= [5,6,7,8]     #list
#the map() method applies the lambda function to all the elements of the tuple num1 and list num2 parallelly
sqrd_obj = map(lambda n1,n2: n1+n2, num1, num2 )
print(list(sqrd_obj))   #type-casting the map object to list and printing it

Output:

Multiple Argument Map
Multiple Argument Map Example

Hence we can observe that the lambda function is applied parallelly to the elements of the tuple num1 and list, num2. The output is as expected the sum of the individual elements of the objects passed as parameters.

Note: When iterables with different sizes are passed to the map() method, then the map function is applied to the elements until one of them is exhausted.


Conclusion

So, in this tutorial, we learned about the map() method in Python. The uses of the method as well as the different formats of its use. For any questions feel free to use the comments.


References