Welcome to this lecture in this lecture we're going to learn about list comprehensives which is the
important concept of the titans.
So let's get into it.
So first of all what is the list comprehension and how it differs from the follow.
So if I make any example over here so let me say for a yucks in Raines when we know the Raines function
and how it works.
So let me say Raines 20 and here I'll make the condition here.
If he X is modulus of two is equal equal zero then let me just print my yes who is obviously you see
from this score well game going to get only the even number from zero to 19.
And let me just print this and it is going to get us Gerald waiting a basically 19 is our number.
So it is going to print at 0 and 2 3 in all of this which is obviously the even number.
But if you want to do this with the list comprehension it will be more readable because it will be executed
in a single line.
So instead of making all of this line it will be only the single line.
So let's make the list comprehension in action.
So let's see how this works.
So first of all I'll say no.
And I'll say it is even number.
So all you need to see here is a list comprehension operation will this result us or that is a list.
So everything you do here will result the list.
That means we have to have the list over here.
So let's do the operation.
So first of all here will be the expression.
So what do you want to written.
And after that you'll have the loop.
And after that you will have the condition.
So this is your list comprehension.
So let me just comment it out again.
Let me just get this one and let's go over here.
Unless pasted case would be down here list best this.
So first of all you should be expression of this whole expression here is obviously the X.
We went to the street index which is expression.
After that you should be loop which will be for x in Raines 20 which is our loop.
And the condition now condition here is if Young's Modulus of two is equally call zero.
So this your condition just close your loop this list because this is going to return as a list.
So you have to close the list.
And now lists print my number even lists save this.
Let's remove all of this and let's of this again list preserve 5.
It is going to give us the number.
But this time it is in the form of list because list comprehension will always that is out the list
and we have this returning or printing that list which is the result of the list comprehension.
So you can see instead of this whole three line this single line is more readable because if you just
know about the list comprehension you'll see OK.
This is expression.
And this one is loop and this one is a condition so you can do multiple things with this list comprehension.
So if you see here this expression is a single expression and the loop can be multiple loop and the
conditions here can be also the multiple condition if you say something like if OK I want to add another
condition if X is not equal to maybe zero I want to have zero so let's say this and you can see now
again we have omitted the zero so this part was because of the for loop so let me just remove again
the for loop and let's say this procedure 5.
Now it would just almost zero from our list comprehension is the result because we have added the new
condition saying that or made there is zero if X is not equal zero will just say yes if we x is equal
zero we'll just continue with the loop again doing nothing with the expression.
So this is the list comprehension it is pretty much easy to do.
All you need to seize it is going to return as the list having the expression loop and the condition
it is really easy to do and the conditions can be the multiple you can also add here the new expressions
the new Lu and you can do all of these things in the single line which is the important factor of the
list comprehension making the code more readable for the programmer.
So this will be read for this Celexa so see in the next one.