Writing Your First List Comprehension

Give Feedback

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]

Comments & Discussion

kkruch on March 14, 2019

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

Anonymous on March 14, 2019

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

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!

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.

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!

Dan Bader RP Team on March 21, 2019

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

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.