Loading video player…

Generalized List Comprehension Structure

In this lesson, you saw the advantages of using list comprehensions as well as the general structure or pattern to follow when creating them. You learned that list comprehensions are powerful if used correctly and can lead to more concise and readable code.

Here is an example of the structure of a list comprehension:

(values) = [(expression) for (value) in (collection)]

And the equivalent for loop:

(values) = []
for (value) in (collection):
    (values).append( (expression) )

00:01 Now, if you try and generalize this structure—so, the transformation that we just applied—if you try and generalize that for any other kind of list comprehension, you might end up with a pattern somewhat like this.

00:13 So, what I’ve done here is I’ve taken the list comprehension up here that I used in the example, and I’ve replaced parts of it—kind of, the parts that you would swap out that are not part of the skeleton for a list comprehension, and I replaced them with these markers here.

00:30 So in this case, the (values)which is our output values—would correspond to what was called squares in the example, and then the (expression) would be the x * x operation that we’re doing here.

00:42 And then, of course, this for part would be exactly the same. And we’re taking a (value)—in this case, it would be called x—from a (collection). In this case, it would be the range of integers from 0 to 9.

00:57 This is sort of the template behind this list comprehension. And as you’ve seen, we can take this template and transform it into another template. So, this list comprehension, if you wanted to transform it into a for loop, it would look like this.

01:16 This is pretty much exactly what we’ve done in the example. So again, our (values) here, that’s just squares, and we’re creating an empty list and then we’re iterating over each (value) in the (collection)—and you can see how these correspond here—and then we’re updating the output (values) that we previously initialized.

01:36 We’re just appending stuff to that list, and we’re not just taking the (value) from the (collection) and adding it, but we’re evaluating some kind of (expression) based on that (value) to calculate the actual output value. So, this is how this transformation goes down, and the cool part is that it works in both directions, so you could apply the same transformation to actually take a for loop and turn it into a list comprehension again.

02:03 And that’s kind of the powerful thing about these list comprehensions, because once you’re getting tired of writing loops that look exactly like this—

02:12 well, now you have a tool in your toolbox that you can apply to make these parts in your program a little bit more readable, and basically turn these three lines into a one-liner that does exactly the same thing, which can be pretty powerful. It can be great for improving readability.

02:27 It can also be horrible for improving readability, but I’m going to talk about that in a little bit, at the end. All right, so we just looked at this fairly simple pattern here, and what I want to talk about now is how you can expand this pattern to actually include filtering. And filtering is going to make the list comprehensions that you can write a lot more powerful and a lot more flexible, so let’s take a look at that.

Avatar image for Rob Black

Rob Black on July 12, 2019

Minor point. The terms “values” on the left suggests that each item is a “value” from the right hand side. Of course the left side results from the expression transforming each value on the right, and the narrative does make that clear. But perhaps the text could be revised slightly to reinforce that point:

(resulting values) = [(expression) for (value) in (collection)]


Become a Member to join the conversation.