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.

The map() Function vs Generator Expressions

Give Feedback

In the previous lesson, you covered how to use the map() function in Python in order to apply a function to all of the elements of an iterable and output an iterator of items that are the result of that function being called on the items in the first iterator.

In this lesson, you’ll see how the map() function relates to list comprehensions and generator expressions. You’ll learn that using them is (arguably) more Pythonic than relying on plain map() calls.

00:00 Now, of course, you might be thinking, “Uh, you know what? This looks, kind of… not anything like the usual Python code that I see, or that I would like to write.” And I totally get that. Like, honestly, I would probably not recommend that you write code that looks like this, because it gets confusing.

00:16 It’s very hard for people who are new to Python to understand what’s going on, and it’s kind of a different mindset that you’re in when you’re writing stuff like that.

00:24 So, actually, what you would probably do to make this more Pythonic is you would define a list comprehension, so, something like this. Again, I’m going to define the same fields here—x.nameand then I’m going to add this computed property called 'age' […] x.born.

00:44 And… then, I would just do this for any x in scientists.

00:54 All right! There, you have it. That’s our list comprehension. So, you would do the same thing—typically, you would do it with a list comprehension, because that just kind of gives you a more Pythonic syntax. Now, this isn’t equivalent yet, so we need to make some small changes here. First of all, I would probably do this—if I wanted to make it as close to the map() call, I would do this with a generator expression that I just passed to this tuple() function.

01:30 Like this.

01:33 And then—okay, I can tell we’re doing this in one line now. Then, we can pprint() it—pretty-print it. So, this is from the pprint module, if you haven’t seen the first installments of the series.

01:44 We can just be pprint() it out, and this is what it looks like and we get the same result. So, I would rather write something like this—a generator expression—and then post-process that, or something like this, than this map() call that we had up here, but—it’s kind of personal preference? And both of them, it’s good to know about these things, right?

02:09 It’s good to know how these functional programming principles that are available in most other programming languages—how they correspond to the Python specifics of having a list comprehension here,

02:23 or a generator expression here, in this case, right?

breaves on March 29, 2020

I think map() is still useful because in some cases you need a list to be returned, for example in a pandas dataframe, replacing one column of strings by applying a function that’s more than a one-liner.

Also, I like to write readable python code, so that others can use and maintain my software. I’ve had my python code translated to a dot net framework, and objective-c for iOS. The person doing that translation knows xcode well, but not python at all. Because my code was readable to her, she could do that work independently.

mreed1959 on March 31, 2020

The Lambda expression used in the example is short and quick and does not really expose the power of the map function. Lets say instead you have a dataset that holds a set of waves and you want to run a routine you wrote to smooth out all the waves - this would work well for that.

tas on July 5, 2020

Are there any performance gains in using one over the other, i.e map vs list comprehension?

Dan Bader RP Team on July 5, 2020

It depends on the specific use case but generally they’re about the same speed. This StackOverflow post goes into more detail.

Bresson Nemesis on Aug. 14, 2020

The big difference to me is it’s difficult if not impossible to compose comprehensions. With map(), I can put data through multiple transformers while with comprehensions .... or have I missed something?

Tobias Karlsson on Aug. 21, 2020

When map() is used correctly it is a lot more readable, but I get that the Pythonic way usually is about trying to fit the entire program on one line and I’m not used to that yet coming from other languages.

Consider this instead:

as_name_and_age = lambda it: {'name': it.name, 'age': 2017-it.born}

names_and_ages = map( as_name_and_age, scientists )

You’ll get full benefits of Pythonic duck typing and everything executes exactly as how you read it. For immutability just wrap in tuple.

Become a Member to join the conversation.