1 00:00:00,890 --> 00:00:04,500 Indecision, we are discussing Biton list comprehension. 2 00:00:04,850 --> 00:00:12,290 We shall give you a demonstration to discuss on this very topic and also to share a clear idea on this 3 00:00:12,290 --> 00:00:14,250 Python list comprehension. 4 00:00:14,540 --> 00:00:17,390 So here is the demonstration in the session. 5 00:00:17,390 --> 00:00:23,630 We are discussing liste with comprehensions, so we are discussing multiple different examples to clear 6 00:00:23,630 --> 00:00:25,040 your doubts and conception. 7 00:00:26,120 --> 00:00:30,760 So iterating a string through a for loop and adding the letters to a list. 8 00:00:31,130 --> 00:00:35,900 So initially you are defining one empty list for later in human. 9 00:00:35,900 --> 00:00:37,690 So human is nothing but one string. 10 00:00:38,060 --> 00:00:43,820 So for each and every iteration it will pick up one character from this human string and that will be 11 00:00:43,820 --> 00:00:44,900 assigned to this letter. 12 00:00:45,080 --> 00:00:48,030 And that letter will get appended with this empty list. 13 00:00:48,320 --> 00:00:51,740 So this h underscore letters, dot append letter. 14 00:00:51,860 --> 00:00:56,380 So it will go on appending the characters picked up from this string human. 15 00:00:56,390 --> 00:00:58,190 So let us go for the outcome. 16 00:00:58,210 --> 00:01:00,010 So this is our respective output. 17 00:01:00,350 --> 00:01:07,160 So here we are having this h u m a and so that was this particular for loop has executed four five times 18 00:01:07,310 --> 00:01:12,920 and each and every time it is just going on, adding our respective characters picked up from this list 19 00:01:13,370 --> 00:01:18,860 so I can also put this print within the for loop itself so it will print the output, something like 20 00:01:18,860 --> 00:01:19,190 this. 21 00:01:19,190 --> 00:01:26,000 You can find that at first H has got included, then H you has got included, then M has got included, 22 00:01:26,000 --> 00:01:28,950 then he has got included and last N has got included. 23 00:01:29,300 --> 00:01:31,250 So in this way you are getting this outcome. 24 00:01:32,120 --> 00:01:37,610 We are getting this one and stepwise that how this escalator's has got populated ultimately. 25 00:01:42,640 --> 00:01:46,430 So there is a final outcome, so now we are losing the comprehension. 26 00:01:46,720 --> 00:01:53,700 So later, for later in human, so is later in human again, this particular letter will be picked up, 27 00:01:53,710 --> 00:01:58,440 picking up H u m and the separate characters one at a time. 28 00:01:58,750 --> 00:02:03,730 And that letter will be constitute this very list that is H underscore letter. 29 00:02:03,760 --> 00:02:05,140 So let us go for the execution. 30 00:02:05,150 --> 00:02:11,440 You can find the same Upwood is coming when you are printing this eight underscore letters list so list 31 00:02:11,440 --> 00:02:13,560 compression versus lambda function. 32 00:02:13,960 --> 00:02:16,870 So you know that lambda function is known as anonymous function. 33 00:02:16,870 --> 00:02:18,560 The function will not have any name. 34 00:02:18,970 --> 00:02:25,720 So what we are writing here is that X will be picking up the characters from this particular string 35 00:02:25,720 --> 00:02:30,400 human and this X will be mapped to form this list item. 36 00:02:30,420 --> 00:02:32,080 So now you can go for the execution. 37 00:02:32,080 --> 00:02:36,340 You can find that this letters is nothing but constructing the list. 38 00:02:36,370 --> 00:02:41,650 So here you are doing the typecasting, using the method list here, which will map this particular 39 00:02:41,650 --> 00:02:48,100 lambda function at this lambda function will be will be getting this X and where this X will be picking 40 00:02:48,100 --> 00:02:49,870 up characters from the string human. 41 00:02:50,080 --> 00:02:56,170 And as a result of that, when we are doing this type casting list, this H underscore letters, this 42 00:02:56,170 --> 00:03:00,580 list available, this list object will get populated with these characters. 43 00:03:00,580 --> 00:03:03,460 That is h u m a and accordingly. 44 00:03:05,020 --> 00:03:08,350 So now we are going for if with the list comprehension. 45 00:03:08,770 --> 00:03:10,060 So now what we're doing. 46 00:03:10,210 --> 00:03:13,990 So no list is equal to X, so X will be the member of this. 47 00:03:13,990 --> 00:03:16,690 No list for X in range twenty. 48 00:03:16,700 --> 00:03:23,380 You know that twenty range twenty means that X will be ranging from zero to 19 because 20 will be the 49 00:03:23,560 --> 00:03:25,320 dominating dominating range. 50 00:03:25,450 --> 00:03:26,990 So that is, that is exclusive. 51 00:03:27,010 --> 00:03:33,960 So X will be ranging from zero to 19, but only those X will be considered if X percent is two is is 52 00:03:34,270 --> 00:03:34,640 digital. 53 00:03:34,900 --> 00:03:39,680 That means all the even numbers in between zero to 19 will be considered here. 54 00:03:39,970 --> 00:03:45,600 So print no list so you can find that number listed to print their respective number list here. 55 00:03:45,610 --> 00:03:50,310 So if we go on printing, so I'm getting here to four, up to 18. 56 00:03:50,590 --> 00:03:54,210 So that is the way in which the list has got populated. 57 00:03:54,370 --> 00:03:58,900 There is a list comprehension so nested if with the list comprehension. 58 00:03:59,110 --> 00:04:00,010 So what you are doing. 59 00:04:00,310 --> 00:04:07,000 So y you will be the elements for this new, for this list for y in the range 100, that means that 60 00:04:07,000 --> 00:04:13,900 y you'll be changing from zero to 999 and those Y will be considered, which is, which is divisible 61 00:04:13,900 --> 00:04:20,340 by two and also divisible by five at the same time because Y percent this too is regarded as legitimate. 62 00:04:20,360 --> 00:04:24,160 So the remainder of the division will be zero means will be divisible by two. 63 00:04:24,170 --> 00:04:26,110 Similarly Y will be divisible by five. 64 00:04:26,170 --> 00:04:29,480 That means Y should be divisible by ten here. 65 00:04:29,800 --> 00:04:35,620 So now if you go on executing this code, we are getting the output that is zero, ten, twenty, thirty 66 00:04:35,620 --> 00:04:40,420 up to ninety here because hundred is there is a range maximum limit. 67 00:04:40,540 --> 00:04:47,590 So that means that it will be ranging from Giru to 99, but those will be coming into the consideration 68 00:04:47,590 --> 00:04:54,650 to form the normalised members NUM list elements which will satisfy both the conditions that is Ypersele. 69 00:04:54,650 --> 00:04:57,640 This too is the digital and also Ypersele. 70 00:04:57,640 --> 00:04:59,640 This five is is collegiately here. 71 00:05:00,220 --> 00:05:03,940 So now we are going for the if else with the list comprehension. 72 00:05:04,510 --> 00:05:11,430 So what we are doing so object is even if I pass this too is egotistical ejido else odd. 73 00:05:11,440 --> 00:05:18,080 So either event string or or string will be the member of this object list and where the I will be it 74 00:05:18,100 --> 00:05:22,450 will be in the range then that means I will be ranging from zero to nine so far. 75 00:05:22,450 --> 00:05:27,490 Gittel I percent this to is is will be true so even will be coming. 76 00:05:27,790 --> 00:05:31,770 But for one I percent is two is is will be false. 77 00:05:31,900 --> 00:05:33,210 So else odd. 78 00:05:33,220 --> 00:05:37,750 So all will be coming home for Dev as a member of this, as the element of this list. 79 00:05:37,930 --> 00:05:44,290 So in this way you are getting this event or event or alternatively for I ranging from zero to nine 80 00:05:44,290 --> 00:05:44,520 here. 81 00:05:45,100 --> 00:05:47,980 So now let us go for another very interesting example. 82 00:05:47,980 --> 00:05:51,430 That is a transpose of a matrix using the list comprehension. 83 00:05:51,700 --> 00:05:52,810 So how to do that one? 84 00:05:53,230 --> 00:05:54,880 So metrics matrix are having this one. 85 00:05:54,890 --> 00:05:57,370 So one, two, three, four, five, six and seven. 86 00:05:57,490 --> 00:06:03,850 So here we are having the matrix, which is having we can consider it as a two dimensional it and it 87 00:06:03,850 --> 00:06:07,100 is having four rows and two columns accordingly. 88 00:06:07,390 --> 00:06:12,740 So now if I do, the transpose of it will be having two rows and four columns accordingly. 89 00:06:12,850 --> 00:06:20,530 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 90 00:06:20,530 --> 00:06:24,160 and one only so row I so forth first time. 91 00:06:24,160 --> 00:06:30,130 So I will be having the values zero zero Gedo will be will be populated with the value that is a rule 92 00:06:30,130 --> 00:06:31,210 in Matrix. 93 00:06:31,220 --> 00:06:32,500 There is a rule in Matrix. 94 00:06:32,510 --> 00:06:36,850 So what will happen this Ruggiero will get filled up with a rule in Matrix. 95 00:06:37,120 --> 00:06:39,880 So that means these are matrix we are having here. 96 00:06:39,880 --> 00:06:41,590 We are having this rule I. 97 00:06:42,130 --> 00:06:47,470 Is changing from a zero to one, so initially for the first iteration, I will have zero. 98 00:06:47,860 --> 00:06:55,810 So this one, three, five and seven, that is a zero element of each and every will be taken into into 99 00:06:55,810 --> 00:06:56,560 consideration. 100 00:06:56,560 --> 00:06:59,400 And that will constitute the first rule of this task force. 101 00:06:59,710 --> 00:07:02,910 And when the follow up, I will be one, then draw one. 102 00:07:02,920 --> 00:07:08,450 That means it will picking up that there is a second element from each and every rule there. 103 00:07:08,620 --> 00:07:15,190 So that was two, four, six and eight will be retrieved from this matrix and that will be the second 104 00:07:15,190 --> 00:07:17,160 row content of this task force. 105 00:07:17,290 --> 00:07:23,080 So whenever we are printing this transpose, I'm getting the zero element of each list elements. 106 00:07:23,320 --> 00:07:29,860 That is one three five seven will be coming in the first row and in the second row, that bizzaro index 107 00:07:29,860 --> 00:07:33,760 number one, because the first rule is having the index value zero there. 108 00:07:34,000 --> 00:07:37,750 So it will be picking up the second elements of all this list. 109 00:07:37,780 --> 00:07:41,590 So two, four, six and eight will be picked up and that will be forming. 110 00:07:41,590 --> 00:07:44,950 The second row of this transpose are two dimensional. 111 00:07:45,640 --> 00:07:50,920 So in this way, you have shown you that how the list comprehensions can be done in multiple different 112 00:07:50,920 --> 00:07:51,370 ways. 113 00:07:51,370 --> 00:07:55,060 And we have given some examples to create your concept. 114 00:07:55,600 --> 00:07:56,890 Thanks for watching this video.