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 is it's one of our key terms comprehension.
What are they.
Well they're actually called list comprehension for set comprehension or dictionary comprehension.
And these are some of the data types that we have in Python and we're able to use all 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 the birds chirping in the background.
I think it would be peaceful for you if you get annoyed.
I'm sorry but hey the birds have to have fun as well.
OK.
So what are these comprehension.
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 For character in 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 and 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 param for param in it of all 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 editable.
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 online three I create a list that contains character and hello I do a for 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 ignorable 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 zero to 100 0 0 to ninety nine.
How would I go about doing this again using list comprehension.
I'll say Number Four.
Number.
And then what's the later rule.
I'm going to use.
Well I'm going to use a range and say zero to 100 hundred so that if I click Run here look that I get
a nice long list pre populate it 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 a list where this equals to zero.
This is two.
This is four.
This is six.
This is eight.
This is 10.
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 we'll use the same for
loop that we've seen before like this but instead of just the variable this is not an expression of
how we want to act upon each character just like we saw in lambda expressions.
So I can now say Num Times to for these items.
So now if I do my last three Hey check that out.
That's pretty nice isn't it.
OK let's make it even more challenging.
Let's say my list three.
Now 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 that 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 editable is we're going to loop through things.
And then finally after this we can say f or we can do an if condition so let's say in our case I'll
say if num modulo 2 is equal to zero.
Only then added to the list so if we click run well and make sure that it's less 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 admirable.
We give it to 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 included 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 less 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 tradeoff.
Let's take a break and talk about set and dictionary comprehension in the next video by.