1
00:00:00,000 --> 00:00:00,000
Hello guys.

2
00:00:00,000 --> 00:00:03,000
So we are going to continue our discussion with respect to Python.

3
00:00:03,000 --> 00:00:06,000
And now we are going to start data structures in Python.

4
00:00:06,000 --> 00:00:11,000
Some of the most common data structures that are used in Python programming language are like lists,

5
00:00:11,000 --> 00:00:13,000
tuples, sets and dictionaries.

6
00:00:13,000 --> 00:00:17,000
So in this series of videos, we are probably going to discuss about all these topics.

7
00:00:17,000 --> 00:00:19,000
In this video we are going to focus more on list.

8
00:00:20,000 --> 00:00:23,000
Uh, so let me just go ahead and show you what is the video outline.

9
00:00:23,000 --> 00:00:28,000
So first of all, we will, uh, go ahead and show you how to I will go ahead and show you how to create

10
00:00:28,000 --> 00:00:29,000
a list.

11
00:00:29,000 --> 00:00:35,000
Then I will also show you accessing list items, modifying list items, some of the amazing list methods

12
00:00:35,000 --> 00:00:36,000
that we use.

13
00:00:36,000 --> 00:00:38,000
Then we have operations like slicing list.

14
00:00:39,000 --> 00:00:41,000
How do I how do we iterate over list?

15
00:00:41,000 --> 00:00:43,000
You know by using for loop and all.

16
00:00:43,000 --> 00:00:46,000
Then along with that we'll also be seeing list comprehension.

17
00:00:46,000 --> 00:00:48,000
And then we will be seeing about nested list.

18
00:00:48,000 --> 00:00:52,000
All these things will be covered with lot of practical examples.

19
00:00:52,000 --> 00:00:55,000
And I'll also be talking about what are the common errors that we will be getting.

20
00:00:55,000 --> 00:01:00,000
So before I go ahead, first of all, uh, let us go ahead and just see the definition of list.

21
00:01:00,000 --> 00:01:04,000
So here you can see list are ordered mutable collection of items.

22
00:01:04,000 --> 00:01:05,000
Right.

23
00:01:05,000 --> 00:01:07,000
Ordered mutable collection of items.

24
00:01:07,000 --> 00:01:09,000
What does mutable collection of items basically means?

25
00:01:09,000 --> 00:01:13,000
Uh, like all the items that are present in list, we can actually change it.

26
00:01:13,000 --> 00:01:14,000
Okay.

27
00:01:14,000 --> 00:01:18,000
Um, and they can contain items of different data types also.

28
00:01:18,000 --> 00:01:18,000
Right.

29
00:01:18,000 --> 00:01:22,000
So these are the two main proper properties with respect to list.

30
00:01:22,000 --> 00:01:25,000
Uh, first of all, they are ordered mutable collection of items.

31
00:01:25,000 --> 00:01:28,000
And they can contain items of different different data types.

32
00:01:28,000 --> 00:01:30,000
So let me just go ahead and let me show you that.

33
00:01:30,000 --> 00:01:32,000
How do you define a list?

34
00:01:32,000 --> 00:01:34,000
Let's say I create a variable called as list.

35
00:01:34,000 --> 00:01:37,000
And if I use this two brackets.

36
00:01:37,000 --> 00:01:40,000
And before that don't forget to select a kernel.

37
00:01:40,000 --> 00:01:43,000
So right now I've selected the kernel based on the environment that I've actually created.

38
00:01:43,000 --> 00:01:49,000
So here you can see over here when I write list with some this empty square braces.

39
00:01:49,000 --> 00:01:57,000
And if I go ahead and print the type of this variable this will give us an empty list.

40
00:01:57,000 --> 00:01:58,000
Right.

41
00:01:58,000 --> 00:02:00,000
So this is a class type of list.

42
00:02:00,000 --> 00:02:03,000
So in order to create a list it is very much simple.

43
00:02:03,000 --> 00:02:06,000
You can just use this empty brackets, right?

44
00:02:07,000 --> 00:02:12,000
Um, now if I want to add elements in the list, uh, let's say that I want to go ahead and create it.

45
00:02:12,000 --> 00:02:15,000
Like I may have multiple names over here.

46
00:02:15,000 --> 00:02:23,000
Let's say I have Krish, I have, um, Jack, I have Jacob.

47
00:02:23,000 --> 00:02:24,000
Right.

48
00:02:24,000 --> 00:02:26,000
So this is basically a list of names.

49
00:02:26,000 --> 00:02:27,000
Okay.

50
00:02:27,000 --> 00:02:31,000
I may also have other elements, like I may have one, two, three, four, five.

51
00:02:31,000 --> 00:02:33,000
Any number of elements.

52
00:02:33,000 --> 00:02:36,000
I may also have different different data types of elements over here.

53
00:02:36,000 --> 00:02:39,000
So here if I go ahead and go ahead and print names.

54
00:02:40,000 --> 00:02:44,000
So it is giving me a name error because I did not write names.

55
00:02:44,000 --> 00:02:47,000
So here you can see that I'm able to get a list of elements right.

56
00:02:47,000 --> 00:02:49,000
So this is how a list is.

57
00:02:49,000 --> 00:02:52,000
Let's say that I go ahead and create a mixed list.

58
00:02:52,000 --> 00:02:55,000
Mixed mixed underscore list list.

59
00:02:55,000 --> 00:03:02,000
And here let's say my first is a number, second is a string, third is a floating point right.

60
00:03:02,000 --> 00:03:04,000
And fourth is a Boolean variable.

61
00:03:04,000 --> 00:03:04,000
Right.

62
00:03:04,000 --> 00:03:07,000
So I may define any types of data type.

63
00:03:07,000 --> 00:03:08,000
And I may also print this.

64
00:03:08,000 --> 00:03:12,000
So let me just go ahead and print mixed underscore list.

65
00:03:12,000 --> 00:03:16,000
So I hope you got an idea about how to properly create a list of items.

66
00:03:16,000 --> 00:03:16,000
Right.

67
00:03:16,000 --> 00:03:21,000
So one is directly using this particular empty brackets.

68
00:03:21,000 --> 00:03:23,000
Or you can also go ahead and add elements over here.

69
00:03:24,000 --> 00:03:28,000
Um with respect to and any, any kind of elements, any different different data types of elements you

70
00:03:28,000 --> 00:03:29,000
can basically add.

71
00:03:29,000 --> 00:03:30,000
Okay.

72
00:03:30,000 --> 00:03:36,000
Now let's talk about accessing accessing list items.

73
00:03:36,000 --> 00:03:39,000
How do we access the list elements.

74
00:03:39,000 --> 00:03:40,000
Right.

75
00:03:40,000 --> 00:03:43,000
So let's say that I uh I create a fruits, right?

76
00:03:43,000 --> 00:03:47,000
Let's say that I'm going to create a fruits element like a list of fruits.

77
00:03:47,000 --> 00:03:49,000
Let's say like apple.

78
00:03:49,000 --> 00:04:00,000
It can be banana, it can be cherry, it can be um, kiwi.

79
00:04:00,000 --> 00:04:06,000
It can be um, pomegranate.

80
00:04:06,000 --> 00:04:06,000
Pomegranate.

81
00:04:06,000 --> 00:04:06,000
Okay.

82
00:04:07,000 --> 00:04:09,000
Or let me just write a simple.

83
00:04:10,000 --> 00:04:11,000
Okay, let's go ahead and see.

84
00:04:11,000 --> 00:04:13,000
Uh, cherries already written.

85
00:04:13,000 --> 00:04:14,000
Um, Apple.

86
00:04:14,000 --> 00:04:14,000
Banana.

87
00:04:14,000 --> 00:04:15,000
Cherry.

88
00:04:15,000 --> 00:04:16,000
Kiwi.

89
00:04:16,000 --> 00:04:17,000
Um, okay.

90
00:04:17,000 --> 00:04:20,000
I, I'm not able to remember more fruits names.

91
00:04:20,000 --> 00:04:21,000
Right.

92
00:04:21,000 --> 00:04:23,000
So let us just keep it till here, okay.

93
00:04:24,000 --> 00:04:24,000
Or.

94
00:04:24,000 --> 00:04:25,000
Okay, fine.

95
00:04:25,000 --> 00:04:27,000
I finally I got one guava.

96
00:04:27,000 --> 00:04:28,000
Right.

97
00:04:28,000 --> 00:04:31,000
So I'm just going to go ahead and create this list of elements.

98
00:04:31,000 --> 00:04:33,000
So here is my list of elements over here.

99
00:04:33,000 --> 00:04:37,000
Now uh, let's say that I want to go ahead and display Apple.

100
00:04:37,000 --> 00:04:38,000
Right.

101
00:04:38,000 --> 00:04:42,000
So in order to display it all I have to do is that just go ahead and right roots uh fruits.

102
00:04:42,000 --> 00:04:46,000
And here you can probably see that I can actually use indexing.

103
00:04:46,000 --> 00:04:46,000
Right.

104
00:04:46,000 --> 00:04:51,000
So whenever we create the elements inside a list okay.

105
00:04:51,000 --> 00:04:55,000
So here this first fruit is basically present in the zeroth index.

106
00:04:55,000 --> 00:04:56,000
Then this is present in the first index.

107
00:04:56,000 --> 00:04:57,000
Second index.

108
00:04:57,000 --> 00:04:58,000
Third index fourth index.

109
00:04:58,000 --> 00:05:04,000
So similarly all the indexes uh when I probably say all the indexes in Python starts with zero.

110
00:05:04,000 --> 00:05:09,000
If you know about our programming language, all the indexes over there starts with one number one.

111
00:05:09,000 --> 00:05:09,000
Right.

112
00:05:09,000 --> 00:05:14,000
But here we are specifically going to use um zero is the index over here.

113
00:05:14,000 --> 00:05:14,000
Right.

114
00:05:14,000 --> 00:05:18,000
But uh if I really want to just display Apple right.

115
00:05:18,000 --> 00:05:21,000
So I will just go ahead and write fruit of zero, right.

116
00:05:21,000 --> 00:05:23,000
If I'm actually trying to print this.

117
00:05:23,000 --> 00:05:26,000
So let me just go ahead and write print Fruits of Zero.

118
00:05:26,000 --> 00:05:28,000
It is not nothing but apple.

119
00:05:28,000 --> 00:05:33,000
Let's say I want to probably get fruit of uh, um in middle cherry.

120
00:05:33,000 --> 00:05:34,000
Right.

121
00:05:34,000 --> 00:05:35,000
So this will become 012.

122
00:05:35,000 --> 00:05:39,000
So if I go ahead and write fruits of two.

123
00:05:39,000 --> 00:05:42,000
So I will be able to get apple and cherry.

124
00:05:42,000 --> 00:05:45,000
And there are some amazing techniques with respect to indexing also.

125
00:05:45,000 --> 00:05:48,000
So, uh, let's say that I want to pick up guava, right?

126
00:05:48,000 --> 00:05:50,000
So I can also go ahead and write print.

127
00:05:51,000 --> 00:05:51,000
Um, yeah.

128
00:05:51,000 --> 00:05:55,000
One way is that I can go ahead and take Y01234.

129
00:05:55,000 --> 00:06:00,000
So I can probably go ahead and write fruits off fourth index I will be able to get guava.

130
00:06:00,000 --> 00:06:04,000
Now this go can also be taken through a different way.

131
00:06:04,000 --> 00:06:05,000
So let me just go ahead and write.

132
00:06:05,000 --> 00:06:09,000
So let's say that I'm going to write print of, uh, fruits.

133
00:06:10,000 --> 00:06:14,000
Let me just, uh, see and show it to you over here.

134
00:06:14,000 --> 00:06:19,000
So let me just go ahead and write Fruits of Fruits of.

135
00:06:19,000 --> 00:06:20,000
Okay.

136
00:06:20,000 --> 00:06:23,000
And now in this particular case, let me just go ahead and write minus one.

137
00:06:23,000 --> 00:06:24,000
Let's see what will happen.

138
00:06:24,000 --> 00:06:27,000
See, minus one is nothing, but it is basically talking about the last element itself.

139
00:06:27,000 --> 00:06:28,000
Right.

140
00:06:28,000 --> 00:06:30,000
So I think I should be able to get both right.

141
00:06:30,000 --> 00:06:34,000
So this is the amazing thing with respect to fruits, right.

142
00:06:34,000 --> 00:06:34,000
Uh, sorry.

143
00:06:34,000 --> 00:06:35,000
This list.

144
00:06:35,000 --> 00:06:36,000
Right.

145
00:06:36,000 --> 00:06:41,000
You are able to probably retrieve any fruit or any items inside this list just by using simple indexing.

146
00:06:41,000 --> 00:06:42,000
Okay.

147
00:06:42,000 --> 00:06:47,000
Now let me just show you one more way of printing some set of fruits elements.

148
00:06:47,000 --> 00:06:49,000
So if I go ahead and write fruits okay.

149
00:06:49,000 --> 00:06:56,000
And you know, if I just use the index one okay I'll be able to get banana.

150
00:06:56,000 --> 00:06:57,000
So I'm getting banana zero one.

151
00:06:57,000 --> 00:06:58,000
I'm getting banana over here.

152
00:06:58,000 --> 00:07:00,000
But what if I have a requirement?

153
00:07:00,000 --> 00:07:03,000
I need to get all the elements from banana till guava.

154
00:07:03,000 --> 00:07:03,000
Right.

155
00:07:03,000 --> 00:07:08,000
So if I go ahead and just make one simple change over here, right.

156
00:07:08,000 --> 00:07:11,000
What I will do, I'll say write fruits one colon.

157
00:07:11,000 --> 00:07:11,000
Right?

158
00:07:11,000 --> 00:07:18,000
One colon basically means from one first index till the end index because I'm not giving anything after

159
00:07:18,000 --> 00:07:18,000
the colon.

160
00:07:18,000 --> 00:07:23,000
So if I just go ahead and execute this here you can see banana cherry kiwi guava right.

161
00:07:23,000 --> 00:07:26,000
So this is one way right now let me do one thing.

162
00:07:26,000 --> 00:07:31,000
Let me just go ahead and print fruits one colon.

163
00:07:31,000 --> 00:07:36,000
And let's say I want to get all the elements till this kiwi index one.

164
00:07:36,000 --> 00:07:37,000
So here you have zero one.

165
00:07:37,000 --> 00:07:39,000
So right two three.

166
00:07:39,000 --> 00:07:39,000
Right.

167
00:07:39,000 --> 00:07:43,000
So now if I go ahead and write one colon three, that basically means it is just going to pick up this

168
00:07:43,000 --> 00:07:44,000
first index.

169
00:07:44,000 --> 00:07:44,000
Second index.

170
00:07:44,000 --> 00:07:46,000
It is not going to pick up the key index.

171
00:07:46,000 --> 00:07:47,000
Right.

172
00:07:47,000 --> 00:07:49,000
Uh and now if I go ahead and display it.

173
00:07:49,000 --> 00:07:51,000
So here you can see banana and cherries there.

174
00:07:51,000 --> 00:07:56,000
That basically means the third index is not getting picked up till the third index everything will get

175
00:07:56,000 --> 00:07:56,000
picked up right.

176
00:07:56,000 --> 00:07:59,000
So if I don't write any element over here.

177
00:07:59,000 --> 00:08:01,000
So it is just going to take it completely till the end.

178
00:08:01,000 --> 00:08:02,000
Right.

179
00:08:02,000 --> 00:08:07,000
And uh, here also, uh, what I can actually do, let me just go and show you one more thing.

180
00:08:07,000 --> 00:08:13,000
So if I go ahead and write, print off fruits, uh, minus one colon right now, if I go ahead and execute

181
00:08:13,000 --> 00:08:18,000
this, you can see minus one colon basically means from minus one element till colon.

182
00:08:18,000 --> 00:08:19,000
Right till colon means till the end.

183
00:08:19,000 --> 00:08:22,000
And this is just my last element over there okay.

184
00:08:22,000 --> 00:08:28,000
But if I just go ahead and write -1 or 2 let's say second index.

185
00:08:28,000 --> 00:08:35,000
So here I can see that you have to please observe here minus one is nothing but this specific element

186
00:08:35,000 --> 00:08:35,000
right.

187
00:08:35,000 --> 00:08:37,000
Two is this right.

188
00:08:37,000 --> 00:08:40,000
But I'm not talking about all these elements.

189
00:08:40,000 --> 00:08:44,000
Now if I go ahead and write minus one to -3 or -3.

190
00:08:44,000 --> 00:08:44,000
Right.

191
00:08:44,000 --> 00:08:51,000
Now here also you'll be able to see this indexing is obviously a little bit confusion okay.

192
00:08:51,000 --> 00:08:55,000
There is a little bit of confusion over here because when I'm picking up the minus one element this

193
00:08:55,000 --> 00:08:56,000
is it is giving us.

194
00:08:56,000 --> 00:08:58,000
But I'm saying from -1 to -3, right.

195
00:08:58,000 --> 00:09:00,000
So there is no such element.

196
00:09:00,000 --> 00:09:04,000
And we'll talk more about this kind of examples and how to do this kind of indexing.

197
00:09:04,000 --> 00:09:07,000
Also when we are further discussing about many things as such.

198
00:09:07,000 --> 00:09:09,000
Right, right now don't worry about this.

199
00:09:09,000 --> 00:09:11,000
Otherwise you'll get more confused over here.

200
00:09:11,000 --> 00:09:13,000
Uh, I'll just keep it like this.

201
00:09:13,000 --> 00:09:18,000
I'll remove this particular code for some time and let later on when you'll be see in the series of

202
00:09:18,000 --> 00:09:21,000
videos, will also be talking about those kind of indexing.

203
00:09:21,000 --> 00:09:21,000
Okay.

204
00:09:21,000 --> 00:09:23,000
Now, uh, perfect.

205
00:09:23,000 --> 00:09:23,000
Till here.

206
00:09:23,000 --> 00:09:26,000
I think, uh, we have, uh, learned many things over here.

207
00:09:26,000 --> 00:09:30,000
Now, what I will be showing you is that how you can modify the list elements?

208
00:09:30,000 --> 00:09:31,000
Okay.

209
00:09:31,000 --> 00:09:36,000
So let me just go ahead here and show you modifying the list elements okay.

210
00:09:36,000 --> 00:09:39,000
Now modifying the list elements is very simple.

211
00:09:39,000 --> 00:09:42,000
Let's say that I want this is my fruits right.

212
00:09:42,000 --> 00:09:48,000
So this is my fruits I want to make sure that I replace banana with something else right now.

213
00:09:48,000 --> 00:09:49,000
Banana you know it.

214
00:09:49,000 --> 00:09:50,000
It is in first index.

215
00:09:50,000 --> 00:09:51,000
Right.

216
00:09:51,000 --> 00:09:53,000
So if I go ahead and write this it is in first index.

217
00:09:53,000 --> 00:09:57,000
Let's say from this I want to probably change this into watermelon okay.

218
00:09:58,000 --> 00:10:01,000
And now I will just go ahead and print my fruits.

219
00:10:01,000 --> 00:10:02,000
Right.

220
00:10:02,000 --> 00:10:07,000
So in that specific index I'm writing watermelon over here and I'm actually displaying it.

221
00:10:07,000 --> 00:10:11,000
Now you can see my fruits has changed banana to, uh, to watermelon.

222
00:10:11,000 --> 00:10:12,000
Right.

223
00:10:12,000 --> 00:10:18,000
So this way in the indexing also, you can directly go ahead and probably replace this particular values.

224
00:10:18,000 --> 00:10:19,000
Right.

225
00:10:19,000 --> 00:10:24,000
So this is how you modify uh any elements that is present over here.

226
00:10:24,000 --> 00:10:28,000
Not only that, let's say I want to go ahead and write like this.

227
00:10:28,000 --> 00:10:30,000
Fruits okay.

228
00:10:30,000 --> 00:10:32,000
Fruits one colon okay.

229
00:10:32,000 --> 00:10:34,000
So one colon is basically giving you this answer.

230
00:10:34,000 --> 00:10:39,000
Now if I go ahead and just write here, please replace everything with watermelon.

231
00:10:39,000 --> 00:10:41,000
Now if I go ahead and see fruits.

232
00:10:41,000 --> 00:10:45,000
So here you can see uh, I'm getting this error.

233
00:10:45,000 --> 00:10:47,000
Uh let's see what is the problem.

234
00:10:47,000 --> 00:10:48,000
Uh, watermelon.

235
00:10:48,000 --> 00:10:48,000
Okay.

236
00:10:48,000 --> 00:10:54,000
Now what it has done is that from this particular index, from this particular index, it has replaced

237
00:10:54,000 --> 00:10:55,000
with this particular word.

238
00:10:55,000 --> 00:10:56,000
Right.

239
00:10:56,000 --> 00:10:59,000
But here it is not considered as a word.

240
00:10:59,000 --> 00:11:01,000
Instead, it is taking this as character by character.

241
00:11:01,000 --> 00:11:05,000
We will also be seeing how we can add word to word.

242
00:11:05,000 --> 00:11:05,000
Right?

243
00:11:05,000 --> 00:11:10,000
So in in in list you have some amazing methods like append and all.

244
00:11:10,000 --> 00:11:12,000
We'll be talking about it as we go ahead right now okay.

245
00:11:12,000 --> 00:11:14,000
Just after this section itself.

246
00:11:14,000 --> 00:11:14,000
Right.

247
00:11:14,000 --> 00:11:17,000
So this is one of the problem that we see right now.

248
00:11:17,000 --> 00:11:18,000
Let me do one thing.

249
00:11:18,000 --> 00:11:22,000
Let me again, uh, use that same fruits over here that I've actually defined.

250
00:11:23,000 --> 00:11:23,000
Okay.

251
00:11:23,000 --> 00:11:25,000
But you understood the problem.

252
00:11:25,000 --> 00:11:27,000
What has happened over here as soon as I said one colon.

253
00:11:27,000 --> 00:11:28,000
Right.

254
00:11:28,000 --> 00:11:31,000
So from first index it has just added w a t r.

255
00:11:31,000 --> 00:11:34,000
So it is not just considered this as a single word.

256
00:11:34,000 --> 00:11:37,000
Instead it is considering character by character and it is adding it.

257
00:11:37,000 --> 00:11:40,000
Okay, so let me just go ahead and define my fruits again.

258
00:11:40,000 --> 00:11:40,000
Okay.

259
00:11:40,000 --> 00:11:47,000
Now let me just go ahead and discuss more about amazing thing that is called as list methods.

260
00:11:47,000 --> 00:11:47,000
Okay.

261
00:11:48,000 --> 00:11:50,000
Now this list methods is quite beautiful.

262
00:11:50,000 --> 00:11:54,000
We'll see to it and uh, we'll be learning more about it okay.

263
00:11:54,000 --> 00:11:56,000
Now let's say I want to, as I said, right.

264
00:11:56,000 --> 00:12:02,000
I want to append an element inside this okay I want to add one element at the end let's say.

265
00:12:02,000 --> 00:12:05,000
So for this I will go ahead and write fruits dot append.

266
00:12:05,000 --> 00:12:08,000
And here let's say I want to append orange okay.

267
00:12:08,000 --> 00:12:09,000
Okay.

268
00:12:09,000 --> 00:12:14,000
Now once I write that basically means this will add an item okay.

269
00:12:14,000 --> 00:12:15,000
To the end.

270
00:12:16,000 --> 00:12:16,000
Okay.

271
00:12:16,000 --> 00:12:18,000
It will basically add an item to the end.

272
00:12:18,000 --> 00:12:19,000
Okay.

273
00:12:19,000 --> 00:12:25,000
And uh, if I just go ahead and print my fruits, you'll be able to see this.

274
00:12:25,000 --> 00:12:25,000
Right.

275
00:12:25,000 --> 00:12:31,000
So now here we'll be able to see apple, banana, cherry, kiwi, guava and oranges over here.

276
00:12:31,000 --> 00:12:31,000
Right.

277
00:12:32,000 --> 00:12:39,000
Now let's say that I have a requirement that I want to probably append any element at a specific index.

278
00:12:39,000 --> 00:12:39,000
Okay.

279
00:12:39,000 --> 00:12:50,000
So let's say I want to add, um, let's say I want to add watermelon in this first index okay.

280
00:12:50,000 --> 00:12:51,000
And right now in the first index banana is there.

281
00:12:51,000 --> 00:12:57,000
So what I will do I'll say fruits dot insert okay insert.

282
00:12:57,000 --> 00:12:59,000
Insert and I will give the index number.

283
00:12:59,000 --> 00:13:00,000
The first element will be the index number.

284
00:13:00,000 --> 00:13:05,000
And the second element will be whatever item I want to put let's say banana okay.

285
00:13:05,000 --> 00:13:09,000
And then I'm just going to print fruits.

286
00:13:10,000 --> 00:13:13,000
So here you can see apple banana banana okay.

287
00:13:13,000 --> 00:13:15,000
Banana was already there I guess.

288
00:13:15,000 --> 00:13:16,000
Yeah banana was there.

289
00:13:16,000 --> 00:13:18,000
Let me do one thing.

290
00:13:18,000 --> 00:13:21,000
Let me add watermelon also.

291
00:13:21,000 --> 00:13:21,000
Okay.

292
00:13:21,000 --> 00:13:24,000
So so in the first index I'm just going to add watermelon.

293
00:13:24,000 --> 00:13:29,000
So here you can see apple watermelon banana banana banana because I have added one time right.

294
00:13:29,000 --> 00:13:31,000
Let's say that I hate two bananas are there.

295
00:13:31,000 --> 00:13:32,000
I want to remove this.

296
00:13:32,000 --> 00:13:35,000
So I will go ahead and write fruits dot remove.

297
00:13:35,000 --> 00:13:39,000
So remove is another inbuilt function uh that is present inside this.

298
00:13:39,000 --> 00:13:41,000
So here I'm going to basically write banana okay.

299
00:13:41,000 --> 00:13:46,000
And now if I go ahead and print fruits okay.

300
00:13:46,000 --> 00:13:50,000
So here you can see banana has gone right now but we don't have one more additional banana.

301
00:13:51,000 --> 00:13:51,000
Okay.

302
00:13:52,000 --> 00:14:03,000
Uh, let's say I want to also remove and um, let's say uh, see whenever we are using this remove.

303
00:14:03,000 --> 00:14:03,000
Right.

304
00:14:03,000 --> 00:14:13,000
It is basically removing the first occurrence of an item.

305
00:14:13,000 --> 00:14:19,000
Right now, let's say I want to remove and return the last item.

306
00:14:20,000 --> 00:14:22,000
Return the last item.

307
00:14:24,000 --> 00:14:24,000
Okay.

308
00:14:26,000 --> 00:14:28,000
Let's say I want to do this.

309
00:14:28,000 --> 00:14:30,000
Uh, what do I mean by this?

310
00:14:30,000 --> 00:14:35,000
Let's say I go ahead and use one amazing inbuilt function which is called as pop.

311
00:14:35,000 --> 00:14:42,000
Okay, now pop basically means it is just going to pop the last element that is present in that particular

312
00:14:42,000 --> 00:14:42,000
fruit.

313
00:14:43,000 --> 00:14:44,000
Uh, and it is going to return us.

314
00:14:44,000 --> 00:14:45,000
Right.

315
00:14:45,000 --> 00:14:49,000
So here I'm going to write pop underscore fruit okay.

316
00:14:49,000 --> 00:14:50,000
Is equal to.

317
00:14:50,000 --> 00:14:52,000
And let me just go ahead and print it.

318
00:14:52,000 --> 00:14:54,000
So I'm going to print it over here.

319
00:14:54,000 --> 00:14:58,000
And let me just go ahead and write pop fruits okay.

320
00:14:58,000 --> 00:15:00,000
And, uh, let me just print it.

321
00:15:00,000 --> 00:15:00,000
Okay.

322
00:15:00,000 --> 00:15:02,000
Let's, uh.

323
00:15:02,000 --> 00:15:07,000
Because at the end of the day, once, I probably, uh, print this and I'll also print the remaining

324
00:15:07,000 --> 00:15:09,000
items that are there in the fruits.

325
00:15:10,000 --> 00:15:10,000
Okay.

326
00:15:10,000 --> 00:15:12,000
So here you can see orange has been popped.

327
00:15:12,000 --> 00:15:12,000
Why?

328
00:15:12,000 --> 00:15:16,000
Because the last element in this list of fruits was orange.

329
00:15:16,000 --> 00:15:20,000
When orange was popped from this particular orange got removed.

330
00:15:20,000 --> 00:15:23,000
Okay, remove and return the last element.

331
00:15:24,000 --> 00:15:25,000
That is how you can specifically do it.

332
00:15:25,000 --> 00:15:28,000
And for that we use an inbuilt function called as pop.

333
00:15:28,000 --> 00:15:36,000
Okay, now let's say that I want to get an index of any fruit right based on the name of the fruit.

334
00:15:36,000 --> 00:15:39,000
So if I go ahead and write fruits dot index.

335
00:15:39,000 --> 00:15:46,000
And here let's say that I want to use, uh, cherry, I want to get the index of the fruit called a

336
00:15:46,000 --> 00:15:47,000
cherry.

337
00:15:47,000 --> 00:15:47,000
Okay.

338
00:15:47,000 --> 00:15:48,000
Cherry.

339
00:15:48,000 --> 00:15:51,000
And this will basically be my index.

340
00:15:52,000 --> 00:15:55,000
And let me just go ahead and print the index.

341
00:15:57,000 --> 00:16:02,000
Now, if I go ahead and execute, you can see that cherry is basically present in 012 three third index.

342
00:16:02,000 --> 00:16:05,000
So I am able to get the output okay.

343
00:16:05,000 --> 00:16:09,000
Uh, again one more thing is that, uh, let me just go ahead and do one thing.

344
00:16:09,000 --> 00:16:16,000
Fruit fruit dot append, or I will try to insert in the second index.

345
00:16:16,000 --> 00:16:18,000
Banana okay.

346
00:16:19,000 --> 00:16:20,000
Banana is already there.

347
00:16:20,000 --> 00:16:24,000
And now let me do one one more inbuilt function.

348
00:16:24,000 --> 00:16:26,000
I'll just go ahead and write fruit dot count.

349
00:16:26,000 --> 00:16:28,000
And here I will just go ahead and write.

350
00:16:28,000 --> 00:16:30,000
Sorry, it should be count.

351
00:16:30,000 --> 00:16:34,000
I will just go ahead and write banana okay.

352
00:16:34,000 --> 00:16:34,000
Okay.

353
00:16:34,000 --> 00:16:37,000
And let me just print this.

354
00:16:37,000 --> 00:16:37,000
Okay.

355
00:16:38,000 --> 00:16:43,000
Now what is going to happen is that you'll be able to see it will just count the number of elements.

356
00:16:43,000 --> 00:16:50,000
And obviously after inserting this banana right now, if you see inside my fruits there are two bananas,

357
00:16:50,000 --> 00:16:50,000
right?

358
00:16:50,000 --> 00:16:51,000
Banana and banana.

359
00:16:51,000 --> 00:16:52,000
So it is just counting that.

360
00:16:52,000 --> 00:16:57,000
How many number of items this specific uh, in this particular list it is there, right.

361
00:16:58,000 --> 00:17:04,000
So here I'm just showing you different, different inbuilt function just by using a list of items.

362
00:17:04,000 --> 00:17:08,000
And uh, here you can see I've shown you a lot of coding with respect to this okay.

363
00:17:09,000 --> 00:17:11,000
Now I may also do something like this.

364
00:17:11,000 --> 00:17:13,000
I may sort this entire list of items.

365
00:17:13,000 --> 00:17:16,000
So if I go ahead and write fruits dot sort.

366
00:17:16,000 --> 00:17:18,000
So here you'll be able to see.

367
00:17:18,000 --> 00:17:23,000
Now if I go ahead and print this fruits you'll be seeing apple banana Banana.

368
00:17:23,000 --> 00:17:23,000
Cherry.

369
00:17:23,000 --> 00:17:24,000
Guava.

370
00:17:24,000 --> 00:17:24,000
Kiwi.

371
00:17:24,000 --> 00:17:25,000
Watermelon.

372
00:17:25,000 --> 00:17:27,000
So automatically based on.

373
00:17:27,000 --> 00:17:36,000
So in in short this will this will sort the list in ascending order.

374
00:17:36,000 --> 00:17:37,000
Okay.

375
00:17:37,000 --> 00:17:39,000
It is just going to sort all the list in ascending order.

376
00:17:39,000 --> 00:17:45,000
And finally here you have fruits dot reverse reverse in another function which will reverse all the

377
00:17:45,000 --> 00:17:46,000
elements over here.

378
00:17:46,000 --> 00:17:52,000
Now if I go ahead and do this right now, you can see the all the list of the elements will basically

379
00:17:52,000 --> 00:17:53,000
get reversed, right?

380
00:17:53,000 --> 00:17:58,000
So this is nothing, but it reverse says the list.

381
00:17:58,000 --> 00:17:59,000
Okay.

382
00:17:59,000 --> 00:18:04,000
And just I'm showing you what all different different inbuilt functions are basically present over here.

383
00:18:04,000 --> 00:18:05,000
Okay.

384
00:18:05,000 --> 00:18:11,000
And then uh, finally if I want to clear the entire list I can go ahead and write fruits dot.

385
00:18:11,000 --> 00:18:11,000
Clear.

386
00:18:11,000 --> 00:18:11,000
clear.

387
00:18:12,000 --> 00:18:12,000
Right.

388
00:18:12,000 --> 00:18:15,000
And now if I use this inbuilt function.

389
00:18:15,000 --> 00:18:21,000
So this will remove all items from the list.

390
00:18:22,000 --> 00:18:22,000
Okay.

391
00:18:22,000 --> 00:18:25,000
Now if I go ahead and print fruits.

392
00:18:26,000 --> 00:18:29,000
So here you can see I'm getting an empty list.

393
00:18:29,000 --> 00:18:34,000
So till now we have discussed about mainly uh, if uh, if I go ahead and show you with respect to the

394
00:18:34,000 --> 00:18:39,000
video outline, uh, we have understood about creating list, accessing list items, modifying list

395
00:18:39,000 --> 00:18:40,000
elements.

396
00:18:40,000 --> 00:18:42,000
Uh, then we discussed about list methods.

397
00:18:42,000 --> 00:18:44,000
Now we are going to discuss about slicing list.

398
00:18:44,000 --> 00:18:48,000
So let's go ahead and discuss more about slicing list.

399
00:18:50,000 --> 00:18:53,000
So guys now let's go ahead and talk more about slicing lists.

400
00:18:53,000 --> 00:18:57,000
So before that I will go ahead and make some cells.

401
00:18:57,000 --> 00:18:58,000
Uh so there is a shortcut.

402
00:18:58,000 --> 00:19:04,000
Just press escape and a you will be able to create as many number of cells as you want.

403
00:19:04,000 --> 00:19:04,000
Okay.

404
00:19:04,000 --> 00:19:10,000
Now what I'm actually going to do is that I am just going to take this numbers.

405
00:19:10,000 --> 00:19:10,000
Okay.

406
00:19:10,000 --> 00:19:14,000
So I'm defining these numbers 123456789 ten.

407
00:19:14,000 --> 00:19:17,000
Now I will just go ahead and write some print elements.

408
00:19:17,000 --> 00:19:23,000
Let's say print numbers two colon five okay.

409
00:19:23,000 --> 00:19:28,000
now let's talk about more numbers.

410
00:19:30,000 --> 00:19:30,000
Oops.

411
00:19:34,000 --> 00:19:36,000
Colon five okay.

412
00:19:37,000 --> 00:19:39,000
Okay I didn't close this brackets.

413
00:19:39,000 --> 00:19:41,000
So that is the reason it was giving me an error.

414
00:19:41,000 --> 00:19:42,000
No worries.

415
00:19:43,000 --> 00:19:48,000
Now we'll go ahead and write print numbers five colon.

416
00:19:48,000 --> 00:19:54,000
And along with this we will also go ahead and see print numbers.

417
00:19:55,000 --> 00:19:59,000
Double colon two okay double colon.

418
00:19:59,000 --> 00:20:01,000
So here we are also learning about double colon.

419
00:20:01,000 --> 00:20:03,000
Single colon I've just given you an idea about it.

420
00:20:03,000 --> 00:20:05,000
But we'll also see with respect to double colon.

421
00:20:05,000 --> 00:20:13,000
And then we'll also go ahead and print our numbers double Double colon minus one.

422
00:20:13,000 --> 00:20:14,000
Okay.

423
00:20:14,000 --> 00:20:21,000
Now just pause for a second and just try to imagine in your mind like what will be the specific outputs

424
00:20:21,000 --> 00:20:22,000
okay.

425
00:20:22,000 --> 00:20:24,000
And then we will discuss further okay.

426
00:20:24,000 --> 00:20:26,000
So here you can see two colon five.

427
00:20:26,000 --> 00:20:29,000
It is very simple 012 from 2 to 5 elements.

428
00:20:29,000 --> 00:20:35,000
So two uh 345 is basically getting displayed 012345.

429
00:20:35,000 --> 00:20:37,000
So this three elements will get displayed.

430
00:20:37,000 --> 00:20:40,000
Colon 512345 will get displayed.

431
00:20:40,000 --> 00:20:42,000
So it is basically coming over here till five.

432
00:20:42,000 --> 00:20:46,000
Then you have 6789 from five colon to till the end.

433
00:20:46,000 --> 00:20:48,000
Now this is very much important.

434
00:20:48,000 --> 00:20:49,000
Double colon two.

435
00:20:49,000 --> 00:20:51,000
What does this basically mean.

436
00:20:51,000 --> 00:20:55,000
And double colon two basically means from all the elements from first.

437
00:20:55,000 --> 00:20:58,000
And this two will be taken as a step size.

438
00:20:58,000 --> 00:20:58,000
Right.

439
00:20:58,000 --> 00:21:01,000
So we will jump two steps ahead.

440
00:21:01,000 --> 00:21:04,000
Like from here we'll jump one two then three will get displayed again.

441
00:21:04,000 --> 00:21:06,000
Jump two steps five will display from again.

442
00:21:06,000 --> 00:21:10,000
From this jump to two steps, seven will get displayed again.

443
00:21:10,000 --> 00:21:10,000
Jump over here.

444
00:21:10,000 --> 00:21:12,000
Nine will be getting displayed.

445
00:21:12,000 --> 00:21:13,000
So double colon.

446
00:21:13,000 --> 00:21:16,000
See if I really want to make you understand about double colon.

447
00:21:16,000 --> 00:21:18,000
So I will go ahead and write double colon.

448
00:21:18,000 --> 00:21:20,000
Double colon basically means all the elements.

449
00:21:20,000 --> 00:21:23,000
Right now if I am writing okay fine.

450
00:21:23,000 --> 00:21:26,000
Uh let's give the test step sizes one.

451
00:21:26,000 --> 00:21:26,000
Then.

452
00:21:26,000 --> 00:21:29,000
Also, I'll be getting the same output if I give the step size to two.

453
00:21:29,000 --> 00:21:31,000
So double jumps will be happening.

454
00:21:31,000 --> 00:21:31,000
Right?

455
00:21:31,000 --> 00:21:33,000
This is basically the step size.

456
00:21:33,000 --> 00:21:40,000
Then if I write three then again three steps like from here one two jump and third uh whichever will

457
00:21:40,000 --> 00:21:41,000
be the element that will get displayed.

458
00:21:41,000 --> 00:21:42,000
So four 710 right.

459
00:21:42,000 --> 00:21:44,000
So I hope you got an idea about it.

460
00:21:44,000 --> 00:21:47,000
Now double colon one basically means what.

461
00:21:47,000 --> 00:21:47,000
Okay.

462
00:21:47,000 --> 00:21:52,000
So double colon one double colon is nothing, but it is all the elements minus one okay.

463
00:21:52,000 --> 00:21:55,000
From previous take all the elements one by one and display it.

464
00:21:55,000 --> 00:21:58,000
If I go ahead and again write something like this.

465
00:21:58,000 --> 00:22:01,000
Let's say I go ahead and write numbers okay.

466
00:22:01,000 --> 00:22:03,000
Double colon minus two.

467
00:22:03,000 --> 00:22:06,000
Write some form back every two numbers.

468
00:22:06,000 --> 00:22:06,000
Right.

469
00:22:06,000 --> 00:22:09,000
It will skip every one number and display the other number over here.

470
00:22:09,000 --> 00:22:10,000
Right.

471
00:22:10,000 --> 00:22:12,000
So this is basically called as a slicing.

472
00:22:12,000 --> 00:22:13,000
And uh.

473
00:22:13,000 --> 00:22:15,000
Uh, some of the operations will be involved in slicing.

474
00:22:15,000 --> 00:22:21,000
Whenever we want to retrieve some results we want to modify them, do something right so we can actually

475
00:22:21,000 --> 00:22:27,000
use it right now coming to the next thing is iterating over list.

476
00:22:27,000 --> 00:22:28,000
Okay.

477
00:22:28,000 --> 00:22:31,000
Now I have already shown you how to iterate over strings.

478
00:22:31,000 --> 00:22:35,000
Similarly you can also iterate over list, right?

479
00:22:35,000 --> 00:22:38,000
So for that also again I will be going and using for loop.

480
00:22:38,000 --> 00:22:43,000
Let's say I will go ahead and write for fruit for fruit in fruits.

481
00:22:43,000 --> 00:22:44,000
Right.

482
00:22:44,000 --> 00:22:48,000
And I will just go ahead and inside and print all the fruit items that I have.

483
00:22:49,000 --> 00:22:49,000
Right.

484
00:22:49,000 --> 00:22:53,000
So here one by one right now no fruit items is present or what.

485
00:22:53,000 --> 00:22:55,000
Okay, okay I have cleared that fruits.

486
00:22:55,000 --> 00:22:56,000
Right.

487
00:22:56,000 --> 00:22:57,000
So let me do one thing.

488
00:22:57,000 --> 00:23:02,000
Let me just take numbers over here so that I will be able to see through it.

489
00:23:02,000 --> 00:23:02,000
Right.

490
00:23:02,000 --> 00:23:05,000
For number in numbers.

491
00:23:05,000 --> 00:23:06,000
Right.

492
00:23:06,000 --> 00:23:08,000
So numbers is also a collection over here.

493
00:23:08,000 --> 00:23:10,000
List of collection list of elements.

494
00:23:10,000 --> 00:23:13,000
So 123456789 ten okay.

495
00:23:13,000 --> 00:23:14,000
So this is also there.

496
00:23:14,000 --> 00:23:18,000
Let's say I want to iterate all this list with indexes also.

497
00:23:19,000 --> 00:23:24,000
So I want to iterate iterating with index.

498
00:23:24,000 --> 00:23:31,000
So for this I can use for index, comma, fruit, fruit or index.

499
00:23:31,000 --> 00:23:34,000
Sorry for index comma number in numbers.

500
00:23:34,000 --> 00:23:37,000
Now once I use this numbers right.

501
00:23:37,000 --> 00:23:39,000
Obviously see if I'm directly using this numbers.

502
00:23:39,000 --> 00:23:42,000
I know this is a list, but if I want index information.

503
00:23:42,000 --> 00:23:46,000
Also I have an inbuilt function in Python which is called as enumerate okay.

504
00:23:46,000 --> 00:23:52,000
And if we specifically use this enumerate that basically means we are also going to get the index number.

505
00:23:52,000 --> 00:23:57,000
So if I go ahead and print index comma number, now you'll be able to see all the indexes.

506
00:23:57,000 --> 00:23:59,000
So this is my zeroth index.

507
00:23:59,000 --> 00:24:01,000
The first element is present in the zeroth index.

508
00:24:01,000 --> 00:24:06,000
Then I have two which is present in the first index then three in the second index, four in the third

509
00:24:06,000 --> 00:24:11,000
index, five in the fourth index, six in the fifth index, seven in the sixth index, eighth in the

510
00:24:11,000 --> 00:24:15,000
seventh index, nine in the eighth index, and ten in the ninth index.

511
00:24:15,000 --> 00:24:16,000
So I have all these things.

512
00:24:16,000 --> 00:24:20,000
And just by using this inbuilt function, you get an excess of the index number.

513
00:24:20,000 --> 00:24:24,000
Also along with the list of elements that are present in that particular list.

514
00:24:24,000 --> 00:24:30,000
Okay, so this is one amazing thing about uh, you know, you can also iterate over the list.

515
00:24:30,000 --> 00:24:32,000
You can also iterate along with the index.

516
00:24:32,000 --> 00:24:39,000
Right now let's go ahead and talk about one more amazing thing, which is basically called as list comprehension

517
00:24:39,000 --> 00:24:40,000
okay.

518
00:24:41,000 --> 00:24:43,000
Now list comprehension.

519
00:24:43,000 --> 00:24:47,000
Now guys, let's let's consider that, uh, I want to do something right.

520
00:24:47,000 --> 00:24:55,000
I want to probably, uh, let's say I'll write a for loop for x in range for x in range.

521
00:24:55,000 --> 00:24:57,000
Let's say I'm going to write ten.

522
00:24:58,000 --> 00:25:04,000
And here, uh, what I'm actually going to do is that I'm going to probably create a list element.

523
00:25:04,000 --> 00:25:07,000
Let's say I'm going to create a list empty elements.

524
00:25:07,000 --> 00:25:10,000
And then I will go ahead and write list dot append.

525
00:25:10,000 --> 00:25:13,000
And let me just go ahead and do this okay.

526
00:25:13,000 --> 00:25:15,000
So just imagine what I've actually done.

527
00:25:16,000 --> 00:25:21,000
Uh, here inside this for loop I'm squaring every number and I'm appending in this particular list.

528
00:25:21,000 --> 00:25:21,000
Right.

529
00:25:21,000 --> 00:25:25,000
And finally I can go ahead and print this particular list okay.

530
00:25:25,000 --> 00:25:30,000
So in short this is a code that I've actually written to square every numbers inside this particular

531
00:25:30,000 --> 00:25:31,000
range of numbers.

532
00:25:31,000 --> 00:25:32,000
Right.

533
00:25:32,000 --> 00:25:37,000
So I'm getting 01249 16, 25, 36, 49, 64, 81.

534
00:25:37,000 --> 00:25:39,000
Now see imagine in this line of code, right?

535
00:25:39,000 --> 00:25:41,000
I've written 3 to 4 lines right over here.

536
00:25:41,000 --> 00:25:43,000
I have, first of all, initialized an empty list.

537
00:25:43,000 --> 00:25:45,000
Then I'm putting a for loop.

538
00:25:45,000 --> 00:25:47,000
Then I'm performing this specific operation.

539
00:25:47,000 --> 00:25:51,000
What if I can perform this entire list operation?

540
00:25:51,000 --> 00:25:55,000
Because everything is happening inside a list itself, and we are creating a new list also over here,

541
00:25:55,000 --> 00:25:55,000
right?

542
00:25:55,000 --> 00:25:59,000
Obviously, if you're creating your list again, new list over here, again, playing with different

543
00:25:59,000 --> 00:26:01,000
different elements, more memory will be required.

544
00:26:01,000 --> 00:26:06,000
So Python actually helps us to play with list comprehension.

545
00:26:06,000 --> 00:26:14,000
Now in list comprehension it tells you that you can perform all this operation right within a list itself.

546
00:26:14,000 --> 00:26:14,000
Right.

547
00:26:14,000 --> 00:26:16,000
So now see this observe over here.

548
00:26:16,000 --> 00:26:23,000
Now I will go ahead and write for x in range ten.

549
00:26:23,000 --> 00:26:23,000
Right.

550
00:26:23,000 --> 00:26:27,000
So here if I go ahead and execute this uh okay.

551
00:26:27,000 --> 00:26:30,000
Right now it is giving me an error because it is invalid syntax I get it okay.

552
00:26:30,000 --> 00:26:32,000
Now let's let's do this okay.

553
00:26:32,000 --> 00:26:35,000
So first I have written for x in range of ten.

554
00:26:35,000 --> 00:26:37,000
So this is basically iterating through this.

555
00:26:37,000 --> 00:26:40,000
Now the next thing is that what kind of operation I'm performing.

556
00:26:40,000 --> 00:26:42,000
So I want to perform x square.

557
00:26:42,000 --> 00:26:45,000
So here if I go ahead and write x square.

558
00:26:45,000 --> 00:26:51,000
So list comprehension basically has two important things what we want as an output and what is the kind

559
00:26:51,000 --> 00:26:52,000
of operation that we are doing.

560
00:26:52,000 --> 00:26:52,000
Right.

561
00:26:52,000 --> 00:26:54,000
So see this is doing some kind of operation.

562
00:26:54,000 --> 00:26:56,000
So that needs to be displayed.

563
00:26:56,000 --> 00:26:57,000
And this is basically my iteration.

564
00:26:57,000 --> 00:26:59,000
So both of them are combined.

565
00:26:59,000 --> 00:27:03,000
If I go ahead and execute this you will be able to see that in a single list.

566
00:27:03,000 --> 00:27:05,000
I am able to get this specific answer.

567
00:27:05,000 --> 00:27:06,000
So here.

568
00:27:06,000 --> 00:27:06,000
What did I do?

569
00:27:06,000 --> 00:27:12,000
I did not have to create other list, item or list elements as such, right?

570
00:27:12,000 --> 00:27:17,000
All I did is that I probably, uh, you know, executed my code within a line of code.

571
00:27:17,000 --> 00:27:20,000
And what I really want is x square.

572
00:27:20,000 --> 00:27:22,000
I have written it in forward right now.

573
00:27:22,000 --> 00:27:25,000
Let's go ahead and see more examples with respect to list comprehension.

574
00:27:25,000 --> 00:27:27,000
So guys we have just seen one example.

575
00:27:27,000 --> 00:27:30,000
And I hope I know that you may be having some kind of confusion.

576
00:27:30,000 --> 00:27:37,000
So it is a better way that if I really want to make you understand about list comprehension, first

577
00:27:37,000 --> 00:27:43,000
of all, let's talk about a specific syntax like what kind of syntax we specifically use.

578
00:27:43,000 --> 00:27:43,000
Okay.

579
00:27:44,000 --> 00:27:46,000
Now uh, the basic syntax okay.

580
00:27:46,000 --> 00:27:49,000
Let me just make this as a markdown okay.

581
00:27:50,000 --> 00:27:51,000
Markdown over here okay.

582
00:27:51,000 --> 00:27:53,000
Let's comprehension.

583
00:27:53,000 --> 00:27:54,000
Okay.

584
00:27:54,000 --> 00:27:55,000
And this is important.

585
00:27:55,000 --> 00:27:58,000
So that is the reason I'm also going to show you the syntax over here.

586
00:27:58,000 --> 00:27:59,000
Okay.

587
00:27:59,000 --> 00:28:02,000
First of all the basic syntax okay.

588
00:28:03,000 --> 00:28:05,000
Now basic syntax is nothing.

589
00:28:05,000 --> 00:28:11,000
But it looks something like this expression for item in iterable.

590
00:28:11,000 --> 00:28:11,000
Right.

591
00:28:11,000 --> 00:28:16,000
So that is what we have done for item in iterable which is traversing everything.

592
00:28:16,000 --> 00:28:20,000
And this expression is going to perform on all the single items.

593
00:28:20,000 --> 00:28:20,000
Okay.

594
00:28:20,000 --> 00:28:22,000
This is the basic syntax okay.

595
00:28:23,000 --> 00:28:27,000
Now I may also have other option like with conditional logic okay.

596
00:28:27,000 --> 00:28:29,000
If I want to apply some logic over here.

597
00:28:29,000 --> 00:28:33,000
So with conditional logic the expression looks a little bit different.

598
00:28:33,000 --> 00:28:34,000
Okay.

599
00:28:34,000 --> 00:28:36,000
And let me just show you this particular expression also.

600
00:28:36,000 --> 00:28:37,000
Okay.

601
00:28:37,000 --> 00:28:41,000
So here you can see expression for item in iterable if condition.

602
00:28:41,000 --> 00:28:42,000
Also I can put.

603
00:28:42,000 --> 00:28:44,000
So I'm putting if condition over here.

604
00:28:44,000 --> 00:28:46,000
So any kind of conditional logic is there.

605
00:28:46,000 --> 00:28:52,000
You have to probably put it at the last iterate whatever iteration you really want to do, you want

606
00:28:52,000 --> 00:28:55,000
to put in the center and any kind of expression that you want to perform.

607
00:28:55,000 --> 00:28:56,000
You can put it in the starting.

608
00:28:56,000 --> 00:28:57,000
Okay.

609
00:28:57,000 --> 00:29:01,000
Now based on this we will try to see some kind of example.

610
00:29:01,000 --> 00:29:01,000
Okay.

611
00:29:01,000 --> 00:29:05,000
So but remember this this is super super important okay.

612
00:29:05,000 --> 00:29:07,000
And you have to use with respect to this itself.

613
00:29:07,000 --> 00:29:08,000
Right.

614
00:29:08,000 --> 00:29:11,000
So let's consider one example.

615
00:29:11,000 --> 00:29:15,000
So let me go ahead and write basic list comprehension.

616
00:29:18,000 --> 00:29:18,000
Okay.

617
00:29:18,000 --> 00:29:23,000
Let's say I want to do a square off all the number okay.

618
00:29:23,000 --> 00:29:26,000
Square off all the number okay.

619
00:29:26,000 --> 00:29:28,000
Square off a set of numbers.

620
00:29:28,000 --> 00:29:35,000
So I will write for x or for num in range of ten.

621
00:29:35,000 --> 00:29:36,000
Right.

622
00:29:36,000 --> 00:29:40,000
And for every of this number I just need to do num square.

623
00:29:42,000 --> 00:29:42,000
Right.

624
00:29:42,000 --> 00:29:43,000
Square.

625
00:29:46,000 --> 00:29:46,000
Done.

626
00:29:46,000 --> 00:29:52,000
Now if I go ahead and print this in short what I'm actually going to get, I'm going to basically get

627
00:29:52,000 --> 00:29:53,000
the square of this number.

628
00:29:53,000 --> 00:29:57,000
Now what condition I have applied over here basic syntax for item in iterable.

629
00:29:57,000 --> 00:29:59,000
And then I have expression okay.

630
00:29:59,000 --> 00:30:01,000
Now coming to the next one okay.

631
00:30:01,000 --> 00:30:08,000
Let's say let's consider with list comprehension with condition okay a specific condition over here

632
00:30:08,000 --> 00:30:08,000
okay.

633
00:30:08,000 --> 00:30:14,000
And let's say I will try to find out from a list of elements which are which are basically even numbers.

634
00:30:14,000 --> 00:30:15,000
Okay, okay.

635
00:30:15,000 --> 00:30:17,000
Which are even numbers.

636
00:30:17,000 --> 00:30:23,000
Now, let's say if I go ahead and write in for loop for I in range ten, okay.

637
00:30:24,000 --> 00:30:32,000
If I go ahead and write, if I colon two is double equal to zero, right, then what I have to do print.

638
00:30:32,000 --> 00:30:36,000
Or I will probably create a list and I'll append all the even elements.

639
00:30:36,000 --> 00:30:37,000
Right.

640
00:30:37,000 --> 00:30:40,000
So let's say I have created a list an empty list over here.

641
00:30:40,000 --> 00:30:47,000
And now what I'm actually going to do I'm going to basically write list dot append list dot append.

642
00:30:48,000 --> 00:30:51,000
And here I'm going to basically write my I right.

643
00:30:51,000 --> 00:30:58,000
So finally if I go ahead and print my list, you'll be able to see that all the even elements will come

644
00:30:58,000 --> 00:31:00,000
right till this particular ten number.

645
00:31:00,000 --> 00:31:00,000
Right.

646
00:31:01,000 --> 00:31:06,000
But here you need to see how do I convert this into list comprehension.

647
00:31:06,000 --> 00:31:10,000
And I showed you one expression with conditional logic.

648
00:31:10,000 --> 00:31:12,000
Expression is over here for item in iterable is here.

649
00:31:12,000 --> 00:31:17,000
And if condition is over here now the same thing I will try to convert this into list comprehension.

650
00:31:17,000 --> 00:31:22,000
So I will write for I or for num in range.

651
00:31:22,000 --> 00:31:28,000
Range 20 okay or ten okay I have to display the number itself.

652
00:31:28,000 --> 00:31:29,000
So I will right number.

653
00:31:29,000 --> 00:31:31,000
I don't need to even apply any expression.

654
00:31:31,000 --> 00:31:34,000
But here I will go ahead and write one if condition.

655
00:31:34,000 --> 00:31:40,000
I will say hey if num percentile two or modulus two is double equal to zero.

656
00:31:40,000 --> 00:31:45,000
Now this basically becomes my event underscore numbers.

657
00:31:45,000 --> 00:31:48,000
Right now here I do not have to even create a new list.

658
00:31:48,000 --> 00:31:49,000
Right.

659
00:31:49,000 --> 00:31:51,000
All I'm doing it in one single line of code.

660
00:31:51,000 --> 00:31:54,000
So this way my execution will happen quicker, right?

661
00:31:54,000 --> 00:31:55,000
It will be faster.

662
00:31:56,000 --> 00:31:59,000
So I hope you are getting this as an idea.

663
00:31:59,000 --> 00:31:59,000
Okay.

664
00:31:59,000 --> 00:32:01,000
Now one more.

665
00:32:01,000 --> 00:32:02,000
One more thing, is there?

666
00:32:02,000 --> 00:32:02,000
Okay.

667
00:32:02,000 --> 00:32:07,000
One more, very important, uh, thing is there, which is again a type of list comprehension you can

668
00:32:07,000 --> 00:32:11,000
basically say which is called as nested list comprehension.

669
00:32:11,000 --> 00:32:13,000
So let me just go ahead and write it down.

670
00:32:13,000 --> 00:32:22,000
So here you have something called as a nested list comprehension okay.

671
00:32:22,000 --> 00:32:23,000
And it is good to know all these things.

672
00:32:23,000 --> 00:32:28,000
Guys I am trying my level best so that you will be able to understand multiple things.

673
00:32:28,000 --> 00:32:28,000
Okay.

674
00:32:29,000 --> 00:32:35,000
So here you can see and here you can see now what is so different when compared to this okay.

675
00:32:36,000 --> 00:32:40,000
Now here we are iterating through two different iterables.

676
00:32:40,000 --> 00:32:47,000
So for item in iterable one for item two in iterable two, then we are writing an expression.

677
00:32:47,000 --> 00:32:49,000
So that basically means if I have two lists.

678
00:32:49,000 --> 00:32:54,000
And then on top of that two list I want to perform any kind of list comprehension.

679
00:32:54,000 --> 00:32:56,000
Then I can use this nested list comprehension.

680
00:32:56,000 --> 00:32:59,000
So let me show you one perfect example.

681
00:32:59,000 --> 00:32:59,000
Okay.

682
00:32:59,000 --> 00:33:05,000
Let me just go ahead and write it over here and let me write it as nested list.

683
00:33:07,000 --> 00:33:09,000
Comprehension.

684
00:33:09,000 --> 00:33:10,000
Okay.

685
00:33:11,000 --> 00:33:12,000
Comprehension.

686
00:33:14,000 --> 00:33:17,000
So let me go ahead and create my list one.

687
00:33:17,000 --> 00:33:20,000
And this will be 123 okay.

688
00:33:20,000 --> 00:33:29,000
Or I'll take one more element for let me say this is my list two which will be nothing but a.

689
00:33:30,000 --> 00:33:36,000
b, C, d.

690
00:33:36,000 --> 00:33:38,000
Okay, so these are my four elements.

691
00:33:38,000 --> 00:33:40,000
Now let me do one very simple thing.

692
00:33:40,000 --> 00:33:47,000
Okay I will try to make a pair pairs of list one and list two elements okay.

693
00:33:47,000 --> 00:33:51,000
So first of all I will iterate through list one.

694
00:33:51,000 --> 00:33:55,000
So let me just go ahead and write for I in list one okay.

695
00:33:55,000 --> 00:33:59,000
Then I will iterate through list two for j in list two okay.

696
00:33:59,000 --> 00:34:02,000
And then I have to pair it right.

697
00:34:02,000 --> 00:34:07,000
Now for pairing I will be using a tuple I have not discussed yet about a tuple, but I'm just going

698
00:34:07,000 --> 00:34:09,000
to take this I and j and pair it together.

699
00:34:09,000 --> 00:34:11,000
Okay, just write it in this way.

700
00:34:11,000 --> 00:34:13,000
Okay, now you can see.

701
00:34:13,000 --> 00:34:18,000
Or if you don't want to like write like this you can also use list over here Okay, let's see whether

702
00:34:18,000 --> 00:34:19,000
this will work or not.

703
00:34:19,000 --> 00:34:25,000
And now I'm just going to print pair okay.

704
00:34:25,000 --> 00:34:31,000
Appear appear have written pair okay so here you can see one comma one comma b one comma c one comma

705
00:34:31,000 --> 00:34:34,000
d two comma two comma b two comma c two comma d.

706
00:34:34,000 --> 00:34:40,000
So it is just doing all the pairing 1A1B1C1D2A2B2C2D.

707
00:34:40,000 --> 00:34:41,000
So this is what is happening.

708
00:34:41,000 --> 00:34:44,000
First of all this will iterate right with respect to one element.

709
00:34:44,000 --> 00:34:46,000
Then it will iterate all the elements right.

710
00:34:46,000 --> 00:34:49,000
We can also write this in this particular way also.

711
00:34:49,000 --> 00:34:50,000
Right.

712
00:34:50,000 --> 00:34:53,000
But again lot of lines of code we really need to write.

713
00:34:53,000 --> 00:34:55,000
But this is really really powerful.

714
00:34:55,000 --> 00:34:59,000
And here you can actually see with the help of nested list comprehension we are able to do it right

715
00:34:59,000 --> 00:35:01,000
now in Python.

716
00:35:01,000 --> 00:35:04,000
Also you have lot of inbuilt key functions right.

717
00:35:04,000 --> 00:35:07,000
So we can actually use list comprehension.

718
00:35:08,000 --> 00:35:11,000
List comprehension.

719
00:35:11,000 --> 00:35:15,000
With function calls you can create your own custom function and all.

720
00:35:15,000 --> 00:35:21,000
But right now I'm going to show you some uh inbuilt functions okay.

721
00:35:21,000 --> 00:35:23,000
So inbuilt function will try to see.

722
00:35:23,000 --> 00:35:28,000
So list comprehension with this let's say and I will I will show you the code over here.

723
00:35:28,000 --> 00:35:32,000
So here I have words like hello world, Python list comprehension.

724
00:35:32,000 --> 00:35:34,000
Now see everybody focus.

725
00:35:34,000 --> 00:35:37,000
So I'm writing for words in words and I'm just saying length of word.

726
00:35:37,000 --> 00:35:37,000
Right.

727
00:35:37,000 --> 00:35:40,000
So it is just going to give us the length of word over here.

728
00:35:40,000 --> 00:35:41,000
Right.

729
00:35:41,000 --> 00:35:44,000
And how easily we are able to see this particular output.

730
00:35:44,000 --> 00:35:44,000
Right.

731
00:35:44,000 --> 00:35:46,000
And this looks amazing.

732
00:35:46,000 --> 00:35:47,000
Right.

733
00:35:47,000 --> 00:35:51,000
And most of the things are basically very much simple over here right now.

734
00:35:51,000 --> 00:35:58,000
Uh, there is also one more list comprehension technique where you can use else block also.

735
00:35:58,000 --> 00:36:01,000
So, uh, I will give you that as an assignment.

736
00:36:01,000 --> 00:36:02,000
Please make sure that you do it.

737
00:36:02,000 --> 00:36:07,000
But don't worry as we go ahead and run, learn more about the tuples and all there.

738
00:36:07,000 --> 00:36:12,000
Also, we'll be covering more about it, but I hope, uh, you got an idea with respect to list comprehension.

739
00:36:12,000 --> 00:36:17,000
And uh, this was all about the discussion with respect to list.

740
00:36:17,000 --> 00:36:21,000
And, uh, I think I tried my level best to cover almost each and every thing.

741
00:36:21,000 --> 00:36:22,000
Okay.

742
00:36:22,000 --> 00:36:26,000
And at the end of the day, you really need to try more and more to understand more.

743
00:36:26,000 --> 00:36:28,000
Example, unless you don't practice.

744
00:36:28,000 --> 00:36:29,000
Uh, I don't think so.

745
00:36:29,000 --> 00:36:33,000
You will be able to do any perfection in this.

746
00:36:33,000 --> 00:36:33,000
Right.

747
00:36:33,000 --> 00:36:35,000
So more examples, more things.

748
00:36:35,000 --> 00:36:39,000
But at the end of the day, I would like to give the conclusion of this lesson and probably talk about

749
00:36:39,000 --> 00:36:42,000
it, you know, because list is something really important.

750
00:36:42,000 --> 00:36:43,000
So the list.

751
00:36:44,000 --> 00:36:48,000
So here you can see I have given some conclusion with respect to the list comprehension, a powerful

752
00:36:48,000 --> 00:36:50,000
and concise to create list in Python.

753
00:36:50,000 --> 00:36:56,000
They are syntactically syntactically compact and can replace more verbose looping construct.

754
00:36:56,000 --> 00:37:01,000
Understand the syntax of list comprehension will help you write cleaner and more efficient Python code,

755
00:37:01,000 --> 00:37:01,000
right?

756
00:37:01,000 --> 00:37:03,000
So yes, uh, this was it.

757
00:37:03,000 --> 00:37:07,000
Uh, I will see you all in the next video where we will be talking about the next data structure.

758
00:37:07,000 --> 00:37:07,000
Thank.

