Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

What Is the map() Function?

Give Feedback

In this lesson, you’ll see that map() is a built-in function that is one of the functional programming primitives (or building blocks) available in Python. It’s useful in a number of contexts.

map() takes two arguments: an iterable (like a list) and a function. It then applies that function to all of the elements in the iterable. It outputs an iterator of items that are the result of the function called on the items in the first iterator.

00:00 So, again, that’s another built-in function that’s available in Python. You can just go map()—I’m on Python 3, here. Python 2—some of the stuff might be slightly different.

00:11 You’re usually going to get a list object back instead of an iterable—or, an iterator. So, this is maybe something you’ll have to adjust for, but that shouldn’t be too hard. Let’s take a look at the docstring here.

00:22 The map() function returns an iterator that computes the function we pass in—

00:30 this bit here, func—[…] using arguments from each of the iterables. So, basically, we’re passing in an iterable, like a list of something, and then map() is going to apply our function on each item in this list and it’s going to create an output stream, or an output iterator of items that are the results of this function func called on one of the items.

00:55 And we’re stopping once we’re done here. And actually, I think you can pass a number of iterables here and they will all kind of be chained together. We’re not going to be doing that in this tutorial. Okay.

01:05 So, I know this stuff sounds super confusing. The first time I read this, I was like, “Okay, I have no idea what this will be good for and when I would ever use this,” and yeah, I don’t know. It made me feel pretty stupid.

01:19 So what I’m going to do next is I’m going to try and make sure you get some real examples of how this would work, how this would be useful. So, I just wanted to show you this data set that we used in previous installments

01:34 of the series. We have this data set of scientists, these are all namedtuple objects so that they’re immutable. The whole thing is actually stored inside a tuple so that the whole structure is immutable. That’s why I didn’t use a plain list, for example.

01:51 And by immutable, it just means, you know, we can’t reach in and modify these objects. If we wanted to modify one, we’d have to make a copy, and that way we can trace these changes and we can make sure that this list is always going to be the same.

02:04 So, immutability is kind of not having these side effects. It’s kind of a big part of functional programming. Yeah, so this is the dataset we’re working with.

Arif Zuhairi on Sept. 25, 2020

What does it means with it stops when the shortest iterable is exhausted?

Dan Bader RP Team on Sept. 26, 2020

Check out the definition of map() in Python 3:

map(function, iterable, ...)

Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. (Source)

It means that you can pass more than one iterable to map() and in each execution step, map() takes the next element from each iterable and then calls the map function on them.

When one of the iterables has fewer elements than the others, map() will stop “mapping” as soon as the shortest/smallest iterable is exhausted. (“exhausted” meaning that all of its elements have been processed.)

Probably easier to see what I mean in an example:

>>> iterable1 = ["a", "b", "c"]
>>> iterable2 = ["1", "2"]
>>> list(map(lambda a, b: a + b, iterable1, iterable2))
['a1', 'b2']

As you can see, the end result of the map() call in the example above is ['a1', 'b2'] and no exception was raised, even though iterable2 contained fewer elements than iterable1.

I hope that clears it up :) Also check out our dedicated tutorial on Python’s map() function.

Become a Member to join the conversation.