Let's talk about something that's quite unique to Python and makes things really, really nice as Python
programmers, and it's called comprehension.
It's one of our key terms.
Comprehension.
What are they?
Well, they're actually called list comprehension or set comprehension or dictionary comprehension.
And these are some of the data types that we have in Python.
And we're able to use comprehension with these three data types.
By the way, as I'm recording, there are some birds chirping in the background.
I thought it would be nice to just record with birds chirping in the background.
I think it'll be peaceful for you if you get annoyed.
I'm sorry, but hey, the birds have to have fun as well.
Okay, so what are these comprehension as well?
They're a quick way for us to create lists or sets or dictionary in Python instead of perhaps looping
or appending to a bunch of items to lists.
So let me show you, instead of doing something like my list equals an empty list, and from here I'll
do something like four character and hello.
My list dot append.
Character.
Just so I can create this list finally.
So let's say my list and we'll print this out.
If I click Run.
I created a list that contains the characters from Hello.
All right, but.
Is there a faster, cleaner way of doing this?
And yes, there is with list comprehension in Python.
Again, it is quite unique.
You don't see it in a lot of programming languages, but people that use Python really love this feature.
And I do too.
So how can we do this?
Using a list comprehension.
Well, the format we're going to follow is like this.
We're going to say Parab.
For.
Param in iterable.
So when I say param, I mean parameter, or in our case it could be a variable so we can name it whatever
we want.
So let's name this character like we did in here and then character like we did in here.
And then we want an iterable.
In our case, we'll say hello.
So that if I remove this and I click Run.
I get the same thing just like that.
So upon creation on line three, I create a list that contains character and hello.
I do a four loop in here and I create the list.
So the way that I like to think about it is, hey, create a variable and then we're going to do a for
loop saying hey for each variable in the iterable added to the list.
Okay.
Let's expand upon this.
Let's say I wanted to do something else.
Let's call it my list, too.
And I want to create, let's say, a quick list with numbers ranging from 0 to 100 or 0 to 99.
How would I go about doing this?
Again, using list comprehension.
I'll say number four number.
And then what's the rule I'm going to use?
Well, I'm going to use a range and say 0 to 100 so that if I click run here.
Look at that.
I get a nice long list pre-populated with all these numbers.
How cool is that?
Okay.
But here's my challenge to you.
What if I wanted this?
But all the numbers should be multiplied by two.
So I want to list where this equals to zero.
This is two.
This is four.
This is six.
This is eight.
This is ten.
So on and so forth.
How can I do it?
Well, we can actually expand upon this so that my list three this time around will use the same four
loop that we've seen before.
Like this.
But instead of just the variable, this is now an expression of how we want to act upon each character.
Just like we saw in Lambda Expressions.
So I can now say Numb Times to.
For.
These items.
So now if I do my list three.
Hey, check that out.
That's pretty nice, isn't it?
Okay.
Let's make it even more challenging.
Let's say my list three, and let's make this a little bit bigger here.
My list for.
I want to change it so that in my list I still keep here what?
I have the numbers, but I want to only keep the even numbers.
In this case, all of them are, even because they're multiplied by two.
But if, for example, we do to the power of two and let's comment this out for a second and click Run.
We see that some numbers aren't.
Even so, I want to keep these numbers.
But only the even ones.
How would I go about doing it?
Well, in this case, we have a new way of creating this list expression.
We'll do the exact same thing that we've done here.
So I'm just going to copy that.
But at the end, remember, we now have an expression here, something that we're going to do to effect
whatever the item in the iterable is.
We're going to loop through things.
And then finally after this, we can say if or we can do an if condition.
So let's say in our case, I'll say if num.
Modulo two is equal to zero.
Only then add it to the list.
So if we click Run.
Oh.
And make sure that it's list for Click Run.
Look at that.
All of these are even numbers.
So let's look at this format.
Remember.
These are called list comprehension.
It's a way for us to quickly create lists with Python instead of looping and using append or things
that we've seen before.
So it's a shorthand form for us to do things unique like this.
The way we're able to do this is to have an expression here of what we want to do with each item that
we're iterating over.
We loop.
Using a for loop through an iterable, we give it a variable which we're going to act upon, and then
if we want, we also have an option to add a conditional and say, hey, only include it into the list
based on this condition, based on a true or false.
Very, very cool.
Now, although list comprehension are really nice because they're just simple one liners.
It can get pretty confusing looking at this code.
You have to be really familiar with list comprehension to really understand what's going on.
So although this makes code shorter, maybe even cleaner, it does take away a bit of its readability.
So it might be better to just create a descriptive function like generate list of even numbers or something
along those lines.
So it's more descriptive and another developer might have a better idea of what's going on.
So it's always a trade off.
Let's take a break and talk about set and dictionary comprehension in the next video.
Bye bye.