Map Function in Python

How to use the Map Function in Python

Vidya Menon
Sep 11, 2020 · 4 min read
Image for post
Image for post
Photo by Aaron Burden on Unsplash


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

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:

Image for post
Image for post

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

Image for post
Image for post

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.

Image for post
Image for post

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:

Image for post
Image for post

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!


The Startup

Medium's largest active publication, followed by +756K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store