In this tutorial we're going to discuss about the topic list comprehension.
Comprehension is allow us to create sequences from other sequences.
List comprehension are a tool for transforming one list are any AI terrible sequence into another list.
And this is the basic syntax for the list comprehension expression for item in list and a list comprehension.
Returns a list they contest of square brackets containing the expression which needs to be executed
for every element along with the for loop to iterate or word each element using an example we will understand
how this works.
So in our first example we want to create a list list to which is an exact copy of List 1.
So we will try and understand this expression here.
Use of for loop in order to retrieve every element in list 1 and every element in list 1 is denoted
by this variable here.
That is the input variable the variable representing elements of the input sequence so X is for the
element in list 1.
D X here it represents an output expression producing elements of the output list so if you want to
create an exact copy of List 1 This is the expression.
So this means for every element in list 1 The Element X is generated in list 2.
So we will see this in Jupiter known book so here I have defined list 1.
Now I'm going to create lists 2 which is an exact copy of List 1 by using the list comprehension.
So the expression would be for x in list 1.
That means for every element in list 1 I want to generate an element x in list 2.
So let's execute this expression and then display list 2.
So here we have list 2 which is a copy of list 1 now considered another example we want to create a
list to where the elements in list 2 are a square of the elements in list 1.
So let's put the expression in square brackets.
And let's begin with the variable representing the element in the input sequence or x in list 1.
And we want the elements in list 2 to be squared.
So x 2 the password or two.
So this means default low retrieves every element in list 1 and then squares the element and add to
list to execute this expression in Jupiter notebook.
So the expression for list who should be X to the power of to let's put this in parentheses and then
default statement for x in list 1 and then execute the statement and then display this to.
So as you can see using one single line of code we have created list 2 from list 1.
So we have considered two examples for list comprehension.
Now let's discuss the third example.
Read element in list two will be included only if they satisfy a certain condition.
So list comprehension can utilize conditional statements.
So let's say this the same expression that we used for squaring all the elements in list one we are
going to use the same expression.
So X to the power of two.
For x in list 1 and I want to put a condition that only if the element is greater than zero in this
one then only square the element.
So this statement would be if X greater than zero.
So this particular statement checks for the elements with it is greater than zero.
Only if it is greater than zero it is squared and then included in the new list.
So let's execute this expression in Jupiter notebook so here in the Jupiter notebook I'm going to write
the list comprehension statement.
So let's begin with for loop 4D x in list 1 the element in the new list should be squared.
Condition is if X greater than 0.
So let's execute this and then display list 2.
So as you can see there are four elements in list 1 which are greater than 0 and they have the elements
have been squared.
And then the new list.
List 2 has been created.
So this is about list comprehension.
In this tutorial we will discuss about list constructor a list constructor should create a new list
based on the past sequence.
The sequence doesn't need to be a list.
It can be any kind of sequence and this is how did list constructor is denoted list open close parentheses.
And this is the syntax for the list constructor so you can pass an either a bill as a parameter to the
list constructor.
So first I'm going to pass a list containing elements such as 1 2 and 3 and C D output.
Indeed you better known.
So here I have the list constructor and I'm going to pass this sequence one comma too comma 3 and then
execute the same so here.
I have the list creator.
Us now parse a string which is another I travel to the list constructor see I'm going to pass these
string Biden and then execute this as you can see the list constructor has retrieved each of the characters
in the string and then added to the list.
And if you do not pass any parameters to the list constructor and empty list is created.
So this is about list constructor.