Using List Comprehensions Effectively

Dan Bader
Dan Bader 8 Lessons 13m intermediate python

This short course breaks down Python list comprehensions for you step by step. See how Python’s comprehensions can be transformed from and to equivalent “for”-loops so you’ll know exactly what’s going on behind the scenes.

One of my favorite features in Python are list comprehensions. They can seem a bit arcane at first but when you break them down they are actually a very simple construct.

The key to understanding Python’s list comprehensions is that they’re just for-loops over a collection expressed in a more terse and compact syntax.

In the course you’ll also learn when to use list comprehensions in your own Python programs and when not to. Basically, the caveat to Python’s comprehensions is this—as you get more proficient at using them it becomes easier and easier to write code that’s difficult to read.

If you’re not careful you might have to deal with monstrous list, set, dict comprehensions soon. Too much of a good thing is usually a bad thing.

Resources

What’s Included:

Related Learning Paths:

About Dan Bader

Dan Bader is the owner and editor in chief of Real Python and the main developer of the realpython.com learning platform. Dan has been writing code for more than 20 years and holds a master's degree in computer science.

» More about Dan

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

Participant Comments

Avatar image for MAStough

MAStough on Jan. 15, 2022

Thanks for the great, quick overview of list comprehensions.

Avatar image for Glenn Lehman

Glenn Lehman on Oct. 11, 2021

Thank you for putting this together. It helped me to better understand what goes into the decision of when to use a list comprehension.

Avatar image for Eriberto

Eriberto on March 12, 2019

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

← Browse All Courses