Map Function in Python

How to use the Map Function in Python

Vidya Menon
The Startup
4 min readSep 11, 2020

--

Photo by Aaron Burden on Unsplash

Introduction

Map, Filter, and Reduce are typical examples of functional programming. They allow us to write simpler, shorter code, without bothering about loops and branching.

In this tutorial we will see how to use the built-in map and filter functions in python.

Map comes built-in with Python (in the __builtins__ module) and need not be imported. Now let’s get a better understanding of how map works.

Map Function

The map() function has the following syntax:

map(function, *iterables)

Where function is the function on which each element in iterables (as many as they are) would be applied on. The asterisk(*) means there can be as many iterables as possible, function has that exact number as required input arguments. Also, the number of arguments to function must be the number of iterables listed.

Let’s take a look at few examples to get a better understanding:

If I have a list of names (which is the iterable here), and all of which are in upper case and I need to convert them to lower case. In normal python programming, it would be something like this:

Now let’s see how we could do the same by using map function:

Using map() syntax above,where function is str.lower and iterables is the list all_names — just one iterable. Also note that we did not call the str.lower function (as: str.upper), as the map function does that for us on each element in our list.

Please note that in this eg., str.lower function requires only one argument by definition and so we passed just one iterable to it. So, if the function you're passing requires two, or three, or n arguments, then you need to pass in two, three or n iterables to it. Lets take a look at another example to understand this :

If I have a list of numbers containing having four decimal places. And I need to round each element in the list up to its position decimal places, i.e I want to round up the first element in the list to one decimal place, the second element to two decimal places, the third to three decimal places, etc. With map() this is very easy. Let's see how.

Here, the round() function that takes two arguments -- the number to round up and the number of decimal places to round the number up to. So, since the function requires two arguments, we need to pass in two iterables.

The range(1,7) function acts as the second argument to the round() function. So as map iterates through the list deci_numbers, in the first iteration, the first element of the list, 7.156783 is passed along with the first element of range(1,7), 1 to round, making it effectively become round(7.156783,1). During the second iteration, the second element of the list which is 4.546689 along with the second element of range(1,7), 2 is passed to round making it translate to round(4.546689,2). This will continue until the end of the list is reached.

Now in case you change the range to (1,4), result = list(map(round,deci_numbers, range(1,4))), you won't get any error even as the length of deci_numbers and the length of range(1,4) differ. Instead, this is what happens: It takes the first element of deci_numbers and the first element of range(1,4) and passes it to round. round evaluates it then saves the result. Then it goes on to the second iteration, takes the second element in the list and the second element of range(1,4), round saves it again. This goes on till the fourth iteration. In the fourth iteration, it tries to take the fourth element of range(1,4) but since range(1,4) does not have a fourth element, Python simply stops and returns the result, which in this case would simply be [7.2,4.55,15.009].

Using lambda with map function:

Notice here that using lambda and map, I didn’t even need to create a function using the def my_function(). That's how flexible map() is!

Conclusion

In this tutorial, with the help of few examples we learnt how to use the built-in map functions in python.

--

--