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

How to Format Your List Comprehensions

List comprehensions and comprehensions in general can get very long and go over the recommended line length. This lesson showed a couple of ways to break list comprehensions and write them over multiple lines without sacrificing their readability.

The next lessons will talk about the dangers or pitfalls to avoid when working with list comprehensions and when you should choose not to use them at all.

Resources

00:00 Now, there’s a lot more to say about these list comprehensions—or comprehensions in general, because there are also dictionary and set comprehensions, and I’m going to put a link into the description of this video where you can learn about this stuff if you want to.

00:13 But before you run off and jump into refactoring your programs to add list comprehensions to them, I want to talk a little bit about two things. So, number one is formatting: How should you format your list comprehensions so they look nice and clean? Because one downside here is that they can get super long and can kind of go past the line length limit that you might have set for yourself, or if you’re using pep8, you know, it’s really easy to go over that limit.

00:36 So, I want to talk a little bit about that, how I like to format my list comprehensions. And then also I want to give a bit of a caveat here, where list comprehensions seem like a really great tool—and they are—but there’s also a danger of overusing them, so I want to give just my opinion on that, and when it makes sense to use or not use a list comprehension. All right, so in terms of formatting, what I like to do is if I have a slightly more complex list comprehension and it doesn’t fit into one line, often what I’ll do is I’ll align my comprehension that way so that I’ve got the expression on the first line, and then I’ll put the for part below that and I’ll put the filter part on another line.

01:21 I feel like that reads pretty well and looks pretty clean, and it’s a good way to format your list comprehensions if they run the danger of spilling across the line boundary, right? So, that’s a way you could do this.

01:32 To show you an example of what this would look like with the even_squares example that we had previously, I might do something like this—or actually in this case, because I have enough space, I would probably do something like this, and actually leave the filtering down there, or even put it on a single line, because that’s only 56 or 55 characters here, so this is not going to hurt too much.

01:54 But I hope this gives you an idea of what you can do, for example, if you have a longer function call here then it might make sense to format this differently and actually move this down or do something like this.

02:05 Like, basically, what I wanted to say here is that I like using these parts here in the template as my breakpoints if I need them. That can help you break it down if you’re getting close to the line boundary.

Avatar image for Anonymous

Anonymous on March 14, 2019

where is the links as mentioned in Vedio ?

Avatar image for Dan Bader

Dan Bader RP Team on March 14, 2019

Ah, I added the link on the course description and not on the video at first. Here it is: Comprehending Python’s Comprehensions

Become a Member to join the conversation.