Indecision, we are discussing Biton list comprehension.
We shall give you a demonstration to discuss on this very topic and also to share a clear idea on this
Python list comprehension.
So here is the demonstration in the session.
We are discussing liste with comprehensions, so we are discussing multiple different examples to clear
your doubts and conception.
So iterating a string through a for loop and adding the letters to a list.
So initially you are defining one empty list for later in human.
So human is nothing but one string.
So for each and every iteration it will pick up one character from this human string and that will be
assigned to this letter.
And that letter will get appended with this empty list.
So this h underscore letters, dot append letter.
So it will go on appending the characters picked up from this string human.
So let us go for the outcome.
So this is our respective output.
So here we are having this h u m a and so that was this particular for loop has executed four five times
and each and every time it is just going on, adding our respective characters picked up from this list
so I can also put this print within the for loop itself so it will print the output, something like
this.
You can find that at first H has got included, then H you has got included, then M has got included,
then he has got included and last N has got included.
So in this way you are getting this outcome.
We are getting this one and stepwise that how this escalator's has got populated ultimately.
So there is a final outcome, so now we are losing the comprehension.
So later, for later in human, so is later in human again, this particular letter will be picked up,
picking up H u m and the separate characters one at a time.
And that letter will be constitute this very list that is H underscore letter.
So let us go for the execution.
You can find the same Upwood is coming when you are printing this eight underscore letters list so list
compression versus lambda function.
So you know that lambda function is known as anonymous function.
The function will not have any name.
So what we are writing here is that X will be picking up the characters from this particular string
human and this X will be mapped to form this list item.
So now you can go for the execution.
You can find that this letters is nothing but constructing the list.
So here you are doing the typecasting, using the method list here, which will map this particular
lambda function at this lambda function will be will be getting this X and where this X will be picking
up characters from the string human.
And as a result of that, when we are doing this type casting list, this H underscore letters, this
list available, this list object will get populated with these characters.
That is h u m a and accordingly.
So now we are going for if with the list comprehension.
So now what we're doing.
So no list is equal to X, so X will be the member of this.
No list for X in range twenty.
You know that twenty range twenty means that X will be ranging from zero to 19 because 20 will be the
dominating dominating range.
So that is, that is exclusive.
So X will be ranging from zero to 19, but only those X will be considered if X percent is two is is
digital.
That means all the even numbers in between zero to 19 will be considered here.
So print no list so you can find that number listed to print their respective number list here.
So if we go on printing, so I'm getting here to four, up to 18.
So that is the way in which the list has got populated.
There is a list comprehension so nested if with the list comprehension.
So what you are doing.
So y you will be the elements for this new, for this list for y in the range 100, that means that
y you'll be changing from zero to 999 and those Y will be considered, which is, which is divisible
by two and also divisible by five at the same time because Y percent this too is regarded as legitimate.
So the remainder of the division will be zero means will be divisible by two.
Similarly Y will be divisible by five.
That means Y should be divisible by ten here.
So now if you go on executing this code, we are getting the output that is zero, ten, twenty, thirty
up to ninety here because hundred is there is a range maximum limit.
So that means that it will be ranging from Giru to 99, but those will be coming into the consideration
to form the normalised members NUM list elements which will satisfy both the conditions that is Ypersele.
This too is the digital and also Ypersele.
This five is is collegiately here.
So now we are going for the if else with the list comprehension.
So what we are doing so object is even if I pass this too is egotistical ejido else odd.
So either event string or or string will be the member of this object list and where the I will be it
will be in the range then that means I will be ranging from zero to nine so far.
Gittel I percent this to is is will be true so even will be coming.
But for one I percent is two is is will be false.
So else odd.
So all will be coming home for Dev as a member of this, as the element of this list.
So in this way you are getting this event or event or alternatively for I ranging from zero to nine
here.
So now let us go for another very interesting example.
That is a transpose of a matrix using the list comprehension.
So how to do that one?
So metrics matrix are having this one.
So one, two, three, four, five, six and seven.
So here we are having the matrix, which is having we can consider it as a two dimensional it and it
is having four rows and two columns accordingly.
So now if I do, the transpose of it will be having two rows and four columns accordingly.
So how we are doing so so I in two minutes I will have the value zero and one I will be having the zero
and one only so row I so forth first time.
So I will be having the values zero zero Gedo will be will be populated with the value that is a rule
in Matrix.
There is a rule in Matrix.
So what will happen this Ruggiero will get filled up with a rule in Matrix.
So that means these are matrix we are having here.
We are having this rule I.
Is changing from a zero to one, so initially for the first iteration, I will have zero.
So this one, three, five and seven, that is a zero element of each and every will be taken into into
consideration.
And that will constitute the first rule of this task force.
And when the follow up, I will be one, then draw one.
That means it will picking up that there is a second element from each and every rule there.
So that was two, four, six and eight will be retrieved from this matrix and that will be the second
row content of this task force.
So whenever we are printing this transpose, I'm getting the zero element of each list elements.
That is one three five seven will be coming in the first row and in the second row, that bizzaro index
number one, because the first rule is having the index value zero there.
So it will be picking up the second elements of all this list.
So two, four, six and eight will be picked up and that will be forming.
The second row of this transpose are two dimensional.
So in this way, you have shown you that how the list comprehensions can be done in multiple different
ways.
And we have given some examples to create your concept.
Thanks for watching this video.