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.
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.

Comments & Discussion

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.

Become a Member to join the conversation.