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.

When to Use List Comprehensions

Give Feedback

This lesson wraps up the course on list lomprehensions with a word of warning. Comprehensions are a powerful feature of Python but they can lead to unreadable code when overused or when poorly formatted.

You learned when not to use list comprehensions and also what strategies you can apply to help you decide when to use them.

Resources

Comments & Discussion

Eriberto on March 12, 2019

Good presentation of list comprehension, helped me a lot understand the concept and the execution sequence.

Dan Bader RP Team on March 13, 2019

Thanks Eriberto, that’s great to hear! We’ve got some more list comprehension tips in our upcoming Python Interview Tips article.

The Python Tricks book also has a chapter on them if you’re looking to dig deeper :)

govindarajuswaroop on Aug. 9, 2019

HI,

How is list comprehension different from map() function? They seem to do the same functionality.

Dan Bader RP Team on Aug. 9, 2019

List Comprehension vs map() in Python: How is a list comprehension different from the map() function? They seem to do the same functionality.

Great question! I’m going to quote a comment thread from python-guide.org where this was discussed:

PEP 202 says:

List comprehensions provide a more concise way to create lists in situations where map() and filter() and/or nested loops would currently be used.

Source: www.python.org/dev/peps/pep-0202/

Interestingly Guido van Rossum (the creator of Python) says using map with lambda as one of his “regrets” and to use a list comprehension instead. Then in the very next section he says a for loop is more clear than reduce().

  • map(), filter()
  • using a Python function here is slow
  • list comprehensions do the same thing better

  • reduce()

  • nobody uses it, few understand it
  • a for loop is clearer & (usually) faster

Source: www.python.org/doc/essays/ppt/regrets/PythonRegrets.pdf

Also Guido said:

“It has been argued that the real problem here is that Python’s lambda notation is too verbose, and that a more concise notation for anonymous functions would make map() more attractive. Personally, I disagree—I find the list comprehension notation much easier to read than the functional notation, especially as the complexity of the expression to be mapped increases. In addition, the list comprehension executes much faster than the solution using map and lambda. This is because calling a lambda function creates a new stack frame while the expression in the list comprehension is evaluated without creating a new stack frame.”

Source: python-history.blogspot.com/2010/06/from-list-comprehensions-to-generator.html

There’s also a performance difference, please see stackoverflow.com/questions/1247486/python-list-comprehension-vs-map

lordchuffnel on Nov. 12, 2019

Could you please link to or make a video on n-dimensional lists, and list comprehension with lambda.

good video, thank you

Pygator on Dec. 22, 2019

The formula for the general list comprehension with condition and expression is a helpful abstraction.

Become a Member to join the conversation.