Create List Comprehensions
00:00 In this lesson, you’ll learn what list comprehensions are in Python and how you can use them. I’ll start off by heading over to IDLE this time.
00:09
Often you might want to perform an action on each element in a collection. So let’s start off by creating a collection. Again, I will make a numbers
tuple, (1, 2, 3)
, and this could be a list as well.
00:23
It’s just about it being a collection. But let’s stick with the tuple for now. So you have this numbers
tuple, and now you want to square each of the elements in here.
00:32
So you want to perform an operation that goes 1
to the power of 2
, 2
to the power of 2
, and 3
to the power of 2
.
00:39
You can do this using a for
loop, and then you need to also collect the values that you calculated somewhere. A common way of doing that is by first creating an empty list and then collecting all the calculated values in there.
00:54
So I will call the empty list squares
,
00:59
and so I’m just making an empty list here. And then I’ll start my for
loop where I say for
num in numbers:
01:08
Then I want to append to squares
. So I’ll say squares.append()
and I want to append the result of the calculation. So here I want to raise num
to the power of 2
, which in Python you can do by, in this case, writing num
.
01:22
This is the variable that’s going to point to each of the values in the collection, one after another, and then using double star (**
), and then a 2
is going to raise it to the second power.
01:34
So after I run this for
loop, my squares
list is populated with the results of the calculations. 1
to the power of 2
is 1
, 2
to the power of 2
is 4
, et cetera.
01:45 And using this construct works and is perfectly fine, but it’s quite a lot of code to write. And because these types of calculations are quite common, Python has a different construct for it that’s called a list comprehension.
01:58
Now you can, instead of writing these three lines of code, starting off with the initialization of the empty list and then ending with the calculation inside of the for
loop, you can do all of that in a single line.
02:09
So let me show you what this for
loop would look like as a list comprehension.
02:14
Start off by giving a variable name to the list you want to create. And then again, I make an empty list basically. But now instead of sticking with it as an empty list, I’m going to piece into these square brackets the pieces of the for
loop.
02:29 Let me show you how you do that.
02:32
You start off with the calculation you want to perform. So in that case, that would be num
to the power of 2
. Again, just copy it, place it in there.
02:40
So num
to the power of 2
is what I want to do on each element. Next, I just need how you would usually start off the for loop. So num ** 2 for num in numbers
.
02:52
This is what this for
loop would look like as a list comprehension. And if I press Enter, you’ll see that this does the exact same thing.
03:00
So I took each element of the numbers
tuple and squared it, and then appended it to a new list called squares
.
03:14
Let’s recap that on the slides. So a list comprehension is a shorthand for a for
loop. Keep that in mind. That’ll help you to grasp what a list comprehension is.
03:24
And here you again see the for
loop that you’ve seen over in IDLE as well. The construct is that you start off with initializing an empty list, then say for <element> in <collection>:
, and then you do your operations where you append to the empty list that you created before the result of a calculation.
03:42
In this case, it’s raising the number to the power of 2
.
03:47
And the same thing works when you take first the calculation again inside of square brackets and then say for num in numbers
, so the syntax you use for starting a for
loop, after the calculation that you want to perform wrapped inside of square brackets.
04:05
This is what a list comprehension is, and it gives you the same results as using the for
loop. And the result is going to be a list of course, because you’re wrapping this in square brackets here.
04:18 This is what list comprehensions are and how they work. I hope making this connection to for loops helps you to understand what they are and how you can use them to write sometimes more concise code.
04:29 And they’re especially powerful for smaller calculations like the one that you’ve seen now where you want to perform a single operation on all elements in a collection.
04:39 All right, with list comprehensions as part of your tool belt, let’s next look at how you can nest and copy lists. But because nothing sinks in without training, I’ve got a couple of review exercises for you first that you can choose to tackle if you want to.
Become a Member to join the conversation.