1 00:00:02,310 --> 00:00:07,660 In this tutorial we're going to discuss about the topic list comprehension. 2 00:00:08,620 --> 00:00:16,660 Comprehension is allow us to create sequences from other sequences. 3 00:00:16,660 --> 00:00:27,510 List comprehension are a tool for transforming one list are any AI terrible sequence into another list. 4 00:00:27,610 --> 00:00:39,620 And this is the basic syntax for the list comprehension expression for item in list and a list comprehension. 5 00:00:39,670 --> 00:00:49,420 Returns a list they contest of square brackets containing the expression which needs to be executed 6 00:00:49,480 --> 00:00:58,390 for every element along with the for loop to iterate or word each element using an example we will understand 7 00:00:58,390 --> 00:01:00,560 how this works. 8 00:01:00,670 --> 00:01:12,330 So in our first example we want to create a list list to which is an exact copy of List 1. 9 00:01:12,820 --> 00:01:17,640 So we will try and understand this expression here. 10 00:01:17,760 --> 00:01:31,750 Use of for loop in order to retrieve every element in list 1 and every element in list 1 is denoted 11 00:01:31,750 --> 00:01:34,270 by this variable here. 12 00:01:34,270 --> 00:01:46,290 That is the input variable the variable representing elements of the input sequence so X is for the 13 00:01:46,290 --> 00:01:48,440 element in list 1. 14 00:01:48,600 --> 00:02:00,240 D X here it represents an output expression producing elements of the output list so if you want to 15 00:02:00,240 --> 00:02:04,950 create an exact copy of List 1 This is the expression. 16 00:02:04,950 --> 00:02:14,770 So this means for every element in list 1 The Element X is generated in list 2. 17 00:02:14,940 --> 00:02:23,400 So we will see this in Jupiter known book so here I have defined list 1. 18 00:02:23,430 --> 00:02:34,290 Now I'm going to create lists 2 which is an exact copy of List 1 by using the list comprehension. 19 00:02:34,290 --> 00:02:42,290 So the expression would be for x in list 1. 20 00:02:42,300 --> 00:02:53,340 That means for every element in list 1 I want to generate an element x in list 2. 21 00:02:53,550 --> 00:02:58,200 So let's execute this expression and then display list 2. 22 00:02:58,770 --> 00:03:11,240 So here we have list 2 which is a copy of list 1 now considered another example we want to create a 23 00:03:11,240 --> 00:03:19,520 list to where the elements in list 2 are a square of the elements in list 1. 24 00:03:19,520 --> 00:03:23,950 So let's put the expression in square brackets. 25 00:03:26,000 --> 00:03:36,350 And let's begin with the variable representing the element in the input sequence or x in list 1. 26 00:03:36,680 --> 00:03:40,880 And we want the elements in list 2 to be squared. 27 00:03:40,880 --> 00:03:46,010 So x 2 the password or two. 28 00:03:46,040 --> 00:03:56,210 So this means default low retrieves every element in list 1 and then squares the element and add to 29 00:03:56,270 --> 00:04:02,130 list to execute this expression in Jupiter notebook. 30 00:04:02,350 --> 00:04:13,300 So the expression for list who should be X to the power of to let's put this in parentheses and then 31 00:04:13,300 --> 00:04:25,540 default statement for x in list 1 and then execute the statement and then display this to. 32 00:04:25,750 --> 00:04:36,400 So as you can see using one single line of code we have created list 2 from list 1. 33 00:04:36,420 --> 00:04:41,580 So we have considered two examples for list comprehension. 34 00:04:41,580 --> 00:04:44,210 Now let's discuss the third example. 35 00:04:45,440 --> 00:04:54,910 Read element in list two will be included only if they satisfy a certain condition. 36 00:04:55,480 --> 00:05:00,490 So list comprehension can utilize conditional statements. 37 00:05:00,700 --> 00:05:08,570 So let's say this the same expression that we used for squaring all the elements in list one we are 38 00:05:08,560 --> 00:05:11,560 going to use the same expression. 39 00:05:11,560 --> 00:05:16,060 So X to the power of two. 40 00:05:16,060 --> 00:05:29,680 For x in list 1 and I want to put a condition that only if the element is greater than zero in this 41 00:05:29,680 --> 00:05:32,710 one then only square the element. 42 00:05:33,490 --> 00:05:39,930 So this statement would be if X greater than zero. 43 00:05:39,940 --> 00:05:47,880 So this particular statement checks for the elements with it is greater than zero. 44 00:05:47,880 --> 00:05:53,790 Only if it is greater than zero it is squared and then included in the new list. 45 00:05:54,060 --> 00:06:03,290 So let's execute this expression in Jupiter notebook so here in the Jupiter notebook I'm going to write 46 00:06:03,620 --> 00:06:07,190 the list comprehension statement. 47 00:06:07,520 --> 00:06:20,370 So let's begin with for loop 4D x in list 1 the element in the new list should be squared. 48 00:06:20,400 --> 00:06:25,610 Condition is if X greater than 0. 49 00:06:25,910 --> 00:06:32,440 So let's execute this and then display list 2. 50 00:06:32,500 --> 00:06:42,070 So as you can see there are four elements in list 1 which are greater than 0 and they have the elements 51 00:06:42,070 --> 00:06:43,050 have been squared. 52 00:06:43,060 --> 00:06:45,440 And then the new list. 53 00:06:45,490 --> 00:06:47,500 List 2 has been created. 54 00:06:47,560 --> 00:06:51,250 So this is about list comprehension. 55 00:06:51,250 --> 00:06:59,670 In this tutorial we will discuss about list constructor a list constructor should create a new list 56 00:07:00,030 --> 00:07:03,090 based on the past sequence. 57 00:07:03,090 --> 00:07:07,140 The sequence doesn't need to be a list. 58 00:07:07,140 --> 00:07:18,400 It can be any kind of sequence and this is how did list constructor is denoted list open close parentheses. 59 00:07:18,540 --> 00:07:27,870 And this is the syntax for the list constructor so you can pass an either a bill as a parameter to the 60 00:07:27,870 --> 00:07:29,320 list constructor. 61 00:07:29,370 --> 00:07:39,930 So first I'm going to pass a list containing elements such as 1 2 and 3 and C D output. 62 00:07:39,930 --> 00:07:40,960 Indeed you better known. 63 00:07:41,220 --> 00:07:49,860 So here I have the list constructor and I'm going to pass this sequence one comma too comma 3 and then 64 00:07:49,860 --> 00:07:52,710 execute the same so here. 65 00:07:52,740 --> 00:07:55,290 I have the list creator. 66 00:07:55,400 --> 00:08:03,010 Us now parse a string which is another I travel to the list constructor see I'm going to pass these 67 00:08:03,010 --> 00:08:12,120 string Biden and then execute this as you can see the list constructor has retrieved each of the characters 68 00:08:12,120 --> 00:08:15,990 in the string and then added to the list. 69 00:08:16,020 --> 00:08:23,700 And if you do not pass any parameters to the list constructor and empty list is created. 70 00:08:24,030 --> 00:08:26,610 So this is about list constructor.