Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Populating Lists From Scratch in List Comprehensions

00:00 In the last lesson, you saw how to populate a list using the .append() method inside a for loop. That’s a very important programming structure. In fact, we’ll come back to that in the next lesson.

00:12 But Python has developed a syntax called a list comprehension, which simplifies the code needed to perform that same action. It doesn’t use .append() at all, but it’s important enough that you really should see it in this course while you’re looking at how to populate lists.

00:29 Here’s what that same function would look like using a list comprehension. The first thing you notice is the absence of a for loop. List comprehension syntax does the iteration for you automatically.

00:43 You begin with an expression in brackets, the brackets indicating that you’re creating a list. The first thing inside the brackets is a model of what each expression in the list should look like. And here, since we’re taking the square root of a number, you see the same expression that you saw in the .append() statement in the previous version.

01:08 Taking the square root of a number—that was the expression for each element you were adding to the list.

01:18 Same expression here. That’s followed by the for clause, which looks similar to the for loop header. It identifies what you mean by the variable number in your expression.

01:32 In this case, it is a single element inside the collection, numbers. So this is a list of the square root of number for each number in the collection numbers.

01:49 This script tests it with the same collection of perfect squares, displaying what this function returns.

02:00 This is show_square_roots2.py. You see the same output. This version saves that list to the variable result, which is then returned.

02:14 But it’s simpler to just return the list comprehension expression directly. You can modify this version. I’m not going to save it to a variable,

02:30 I’m just going to return the list built by that expression. Save it.

02:41 It’s the same filename.

02:45 And we can see the same results. List comprehensions are really great. They give you a short, concise way to build a list. But there are times when you really do need to use a for loop, and that’s if you want to do additional processing inside the loop while you populate the list.

03:07 You’ll see that in the next lesson.

Become a Member to join the conversation.