Hello, in this session, we will discuss about list of the list comprehensions are used for creating
new lists from other attributes.
These tables could be list dictionaries.
As list comprehension returns, a list that consists of brackets containing the expression which is
executed for each element, along with the follow through, I read over each element.
So we would be using a simple way of doing the same thing which you were doing using a long before.
You let us see an example.
Let us say we want to find out squares of numbers so we can see for I in range twenty and out of these
numbers we want to find out.
The values such that these are squares of these numbers, so we want to find out.
X, is it for you?
I.
Now, I have these values and I want to add these values to a particular list, so let us create a simple
list.
See, a list is equal to square brackets so that it is an empty list.
Now, I want to obtain this value so I can see enlistee dot up in.
X. So this will offend these values to list and then I can.
Simply print the list for you, so let us run this particular piece of code.
So here you can see that I have iteratively created a list of numbers which has squares of numbers from
zero to 90.
Now, I can do this in a much more simpler manner.
So what I can do is I can simply create another list list, too, and I will define it by the two square
brackets.
And inside that I will give for.
I say it's in range, the same thing, just the way we give it here for I in range 20, this is the
exact same language I am giving you.
And instead of doing all these things, I just need to like.
I spoke with.
Here, this means that for every value in these 20, we need to get values which I today's to the power
to, and then we will something else do for you.
So you can see all the same values are being generated.
It is just the same, but only thing is it is a lot more faster.
It is a lot more faster and simpler.
So we can create a much more complex list also using this, so let's create one.
So let us see.
We want to create the same thing, but this time we want to do it for, just say, even numbers or numbers.
So what we can do is we can do it if.
I.
Is equal to zero, so if I is divisible by two.
So in that case, let us get this so it is returning the squares for only.
Even numbers.
Similarly, we can perform any complex operations, I want to multiply three, do it, and then I won't
do that, they add some numbers to it so I can do any complex operation on top of it and I'll be able
to get the results of what?
So this is the usage of list comprehension and you can try different complex list comprehensions so
you can try out different ranges also.
So let's say we want to generate a particular list there.
I want to get something like a particular number.
Say three things for I in range.
For a few more minutes, let's say, we want to open for.
It went the list.
Sorry, this has to be.
So we can go in any direction that we have different kind of results and use them for.