Loading video player…

Writing Your First List Comprehension

In this lesson, you saw how to write a simple list comprehension that computes the squares of numbers from 0 to 9 and assigns the result to the squares variable.

You also learned that list comprehensions look similar to list constants and that they can be transformed into for loops. The code examples below illustrate this:

List comprehension

>>> squares = [x * x for x in range(10)]
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

For Loop

>>> squares = []
>>> for x in range(10):
...     squares.append(x * x)
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

00:00 Here’s a simple list comprehension that I just defined. You can see here, it looks kind of similar to defining a list constant, right? Where you would just put in values between these square brackets.

00:10 So, I define this list comprehension here, and then assign the result of it to the squares variable. And the reason I did that is so you can see what the output of that list comprehension is. And I also wanted to give it a name because it’s computing all the integer square numbers from 0 to 9.

00:30 And of course, I could have just taken the same list comprehension and run that immediately, right? Now, when you look at this list comprehension, the syntax is fairly human-readable, I want to say.

00:42 And really what the syntax is—it’s kind of a shorthand for a regular for loop. I’m going to clear my screen here and I’m going to bring back the list comprehension.

00:51 And now I’m going to write a for loop that runs the exact same calculation. We’re going to start out with an empty squares list, and we’re going to populate that list as we go along. And then to start the for loop, I’m going to take this part here from the list comprehension.

01:04 I’m just copying and pasting it over.

01:07 And now what I need to do is I need to update squares, so I need to .append() something to it. And for that, I’m going to take this part of the calculation here. All right, so you can see where I copied these parts from, right?

01:20 The squares one—so that one and then the for loop is that one here, that part here in the list comprehension. And then the actual calculation that I’m running here, or the expression that I’m evaluating, I took that from here to calculate the squares.

01:37 All right, so when I take a look at how this squares list was populated by the for loop solution, you can see it’s exactly the same.

01:43 When I rerun the original list comprehension, you can see it has the exact same output. That’s how you transform a very simple list comprehension into a for loop.

Avatar image for kkruch

kkruch on March 14, 2019

It would be great if you have an autoplay feature for videos.

Avatar image for Anonymous

Anonymous on March 14, 2019

i Echo about autoplay. it is annyoing to do play manually rather take it automatically.

Avatar image for Dan Bader

Dan Bader RP Team on March 14, 2019

Thanks folks, autoplay is coming soon! Also, a new feature where you can set the default playback speed for all videos so you can watch at 0.5 - 2.0x speed at your leisure :-) Stay tuned, should be live tomorrow!

Avatar image for Dan Bader

Dan Bader RP Team on March 15, 2019

Quick update—autoplay is live now! You can configure it in your account settings and it is enabled by default.

There’s also a new setting where you can configure the default playback speed for all videos. So if you prefer listening to videos at 0.75x or 1.5x speed you can set this up once in your account settings and it will apply to all lessons.

Avatar image for Peter Ott

Peter Ott on March 21, 2019

What is your python environment? I’m very curious about how to get that auto-complete functionality that you have!

Avatar image for Dan Bader

Dan Bader RP Team on March 21, 2019

@Peter: It’s an alternative REPL for Python called bpython. Such a great tool, highly recommended :)

Avatar image for Omer Faruk

Omer Faruk on April 13, 2019

Thanks for the video :) It would be great if we can continue from the article when we sign in. I am looking at this article. When I sign in, the web site is navigating to the home page however, I want to watch the video or read the article.

Become a Member to join the conversation.