-: Hi guys and welcome back.
In this video, we're going to talk about
list comprehensions in Python.
A list comprehension is a great feature of Python
and allows us to create new lists on the fly
from existing lists.
In addition, it's very succinct and very powerful.
Let's get started.
Here we've got a list of numbers,
and I want to end up with these numbers,
but multiplied by two,
so I'll say something like doubled equal empty list.
How do we put each of these numbers,
multiplied by two, into this new list?
Traditionally and in most languages,
you would end up doing something like for num in numbers,
and then you'd do doubled.append num times two.
This is quite a long piece of code
just for creating a new list of doubled numbers,
but this is how you do it normally.
You would iterate over the numbers list,
create a new variable for each of the numbers in it,
and then run through it three times,
each time putting the appropriate number,
multiplied by two, into your new list.
Python has a much more succinct way of doing this
which is with a list comprehension.
In a list comprehension,
the order of these operations, first the iteration,
and then the appending, seems to be reversed,
but it's not really.
So the first thing you do is you would put in
what you want to add to your new list,
which is num times two.
But of course, num doesn't exist in this context.
It only existed here because we've got our for loop.
So the next thing you do is you put your for loop,
and what this does in Python is it puts num times two
into a new list for every num in the numbers list.
So essentially you're iterating over the numbers list,
and you're putting the variable multiplied by two
every time that the loop runs into your new list.
You end up with the doubled numbers.
Because list comprehension are usually in a single line,
you do want to keep them short if you can,
so instead of num times two for example in this case,
you could do x times two, and for x numbers.
That just helps you keep it a little bit shorter
and a little bit more readable.
After all, the most important thing here
is that you're putting x times two into your new list
while you iterate over the previous list.
So again to recap, how you build a list comprehension
is what you wanna put into your new list
for the variable that you're using in numbers.
Now let's say you've got a friend's list,
and you wanna create a new list
which contains the names of those friends
that start with an s.
So Sam, Samantha, and Saurabh.
But it seems that a list comprehension
may not be the tool for this job.
After all, you do need to only get some of these out.
Let's start with a for loop,
and then I'll show you how the list comprehension
can deal with it.
So you would do for friend in friends.
Then you would add an if statement to check
that the friend name in question starts with an s.
You could say if friend.startswith capital s,
then you're gonna do starts_s.append friend,
and then at the end, we're gonna print starts_s like that.
Let me run this and show you what happens.
Down here, you have Sam, Samantha, and Saurabh,
and I think I'm not pronouncing that right,
so my apologies if that's the case.
So what we've got is a for loop,
and then an if statement, and then an append.
This structure here can also be reproduced
with a list comprehension although it does start to get
a little bit more confusing.
Again, the process is more or less the same.
First of all, you put what you wanna add to your new list,
so that's your friend name.
Then you put your iteration or your for loop,
for friend in friends.
And now if you wanna add an if statement,
you do it at the end,
so you're gonna add your friend
for the friend in your list if the friend starts with s,
and that does exactly the same thing,
and you no longer need that loop there,
so you can save that and run it again,
and you see that the same thing comes out.
So this is how you add a conditional, an if statement,
into a list comprehension.
Very important to nice that
when you use a list comprehension,
a new list is created,
so even though these two lists may be the same
if we take these names out,
so now these two lists will be the same
if we print them both out,
you'll see that they are both identical.
When you print friends is starts_s,
you'll see that false comes out.
That's because these two lists,
although they have the same content,
are not the same list.
They are two different lists, two different places
in your computer's memory with the same values.
For context, friends zero is starts_s a zero,
so Sam is Sam will give you true, see that?
But the lists themselves are not the same,
so the elements in them are the same,
but the lists themselves are not the same,
and by using the is keyword, then you can check that,
but there's also something else you can do,
which is check the IDs of these lists,
so you can do something like friends,
then print out the ID of friends,
and then starts_s and print the ID of starts-s,
and what I've done here by the way
is I have passed multiple different things
to the print function, and you can do that in print,
and it just separates them with a space,
so it's gonna separate these different parts with a space,
the different arguments there.
So by running this, you get that friends has an ID
of this, ends in 2096, and starts_s has an ID
that ends in 5280.
So these two have different IDs, and in Cpython,
which is what most of you will be using,
certainly what I'm using,
the ID is related to the memory address
in which the list is stored,
so you can see that these two things
are in different places in memory.
They're different objects entirely.
They just happen to have the same contents.
So this is something that will be very important
as we proceed with the course,
so this is why I wanted to show it to you at this point.
Creating new lists gives you a different thing
entirely in Python.
It's not the same as the thing before
even if it has the same contents,
but as I mentioned earlier,
if you do want them to be the same exact list,
then you can do this.
Starts_s equal friends, and now when you press play,
you'll see that you get true,
and these IDs are identical.
So that is how you make two lists the same thing.
Otherwise, you do end up with two different lists.
All right, that's enough of that.
Thank you for joining me in this video.
I hope it's been useful,
and I'll see you in the next one.