Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

The map() Function vs Generator Expressions

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?

Avatar image for breaves

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.

Avatar image for mreed1959

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.

Avatar image for tas

tas on July 5, 2020

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

Avatar image for Dan Bader

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.

Avatar image for Bresson Nemesis

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?

Avatar image for Tobias Karlsson

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.

Avatar image for jacquesd

jacquesd on March 22, 2021

Hi,

I was wondering if it’d be possible using these functions to extract block of items in a list and put them into a dictionary?

for instance I have this kind of structure from a QtableWidget.selectedItems():

[attr1, attr2, attr3, attr4, attr5, attr6, attr7, attr8, …]

the 5 first attributes actually represents the first “row” and the 5 columns of the row.

The goal would be to have 1 “item” from the list every 5 “attr” that would be stored in its own list So I’d like to have is something that looks kind of like this:

dict(1: [attr1, attr2, attr3, attr4, attr5], 2: [attr6, attr7, attr8, attr9, attr10], 3: […] )

Do you think, it’d be possible? Thank you.

Best,

Jacques.

Avatar image for jacquesd

jacquesd on March 22, 2021

Don’t bother, I finally found!

Best,

Jacques

Avatar image for waveman

waveman on Nov. 19, 2023

Is there a reason that you don’t use tuple comprehensions vice list one (since it’s functional paradigm)?

Become a Member to join the conversation.