1
00:00:00,000 --> 00:00:01,000
Hello guys.

2
00:00:01,000 --> 00:00:03,000
So we are going to continue the discussion with respect to Python.

3
00:00:03,000 --> 00:00:08,000
And now we are going to discuss about our new data structure which is called as dictionaries.

4
00:00:08,000 --> 00:00:13,000
So in this video this is the entire video outline where we will be discussing about the introduction

5
00:00:13,000 --> 00:00:13,000
to dictionaries.

6
00:00:13,000 --> 00:00:16,000
We will be creating dictionaries with the help of Python.

7
00:00:16,000 --> 00:00:20,000
How to access the dictionary elements, modifying dictionary elements.

8
00:00:21,000 --> 00:00:26,000
Then we'll be seeing some of the amazing dictionary methods that we usually use while we are developing

9
00:00:26,000 --> 00:00:27,000
projects.

10
00:00:27,000 --> 00:00:30,000
Along with that, we'll also be iterating over dictionaries.

11
00:00:31,000 --> 00:00:35,000
Then we will be discussing about nested dictionaries, then dictionary comprehension.

12
00:00:35,000 --> 00:00:40,000
And finally we will be seeing some of the common errors that we usually face.

13
00:00:40,000 --> 00:00:46,000
Now in this it is very much important that we probably do a lot of practicals.

14
00:00:46,000 --> 00:00:50,000
So that is the reason I will be showing you each and every thing with the help of coding.

15
00:00:50,000 --> 00:00:51,000
Now let me go ahead.

16
00:00:51,000 --> 00:00:55,000
And first of all, uh, let's see the definition of dictionaries.

17
00:00:55,000 --> 00:00:59,000
So dictionaries are nothing, but it is an unordered collection of items.

18
00:00:59,000 --> 00:01:02,000
The store data in key value pairs.

19
00:01:02,000 --> 00:01:02,000
Okay.

20
00:01:02,000 --> 00:01:08,000
So they're all the data is basically stored in key value pairs.

21
00:01:08,000 --> 00:01:10,000
Key must be unique and immutable.

22
00:01:10,000 --> 00:01:14,000
So once you specifically define a key it cannot be changed.

23
00:01:14,000 --> 00:01:19,000
Values can be changed and key must always be unique, okay?

24
00:01:19,000 --> 00:01:26,000
And it can include strings, numbers or tuples uh which while values can be of any type okay.

25
00:01:26,000 --> 00:01:30,000
So let me just go ahead and I hope you have just understood the definition.

26
00:01:30,000 --> 00:01:35,000
But again to make you understand much more in a better way, first of all, we will go ahead, uh,

27
00:01:35,000 --> 00:01:38,000
in discussing about creating dictionaries.

28
00:01:38,000 --> 00:01:38,000
Okay.

29
00:01:38,000 --> 00:01:43,000
Now with respect to creating dictionaries, first of all, what we are really going to do over here

30
00:01:43,000 --> 00:01:46,000
is that create an empty dictionary.

31
00:01:46,000 --> 00:01:49,000
Now, already, you know, if I use square braces okay.

32
00:01:49,000 --> 00:01:51,000
And before that make sure you select the kernel.

33
00:01:52,000 --> 00:01:58,000
Uh, so if you are basically using this square brackets then you basically create an empty list.

34
00:01:58,000 --> 00:02:01,000
If you are using tuples you create an empty tuple.

35
00:02:01,000 --> 00:02:05,000
Now in case of dictionary we use flower braces okay.

36
00:02:05,000 --> 00:02:09,000
Now now let's go ahead and define and create our empty dictionary.

37
00:02:09,000 --> 00:02:18,000
So here you can see if I go ahead and print type of empty dictionary, you will be able to see that

38
00:02:18,000 --> 00:02:21,000
I will be getting a class which is called as dictionary.

39
00:02:21,000 --> 00:02:27,000
So in order to create an empty dictionary, all we have to do is that we have to specifically use this

40
00:02:27,000 --> 00:02:27,000
flower braces.

41
00:02:28,000 --> 00:02:28,000
Okay.

42
00:02:28,000 --> 00:02:36,000
Another way how we can actually create our empty dictionary is that by using this dict class okay.

43
00:02:36,000 --> 00:02:39,000
And here we will initialize it as a method.

44
00:02:39,000 --> 00:02:41,000
So once we initialize it here you'll be able to see.

45
00:02:41,000 --> 00:02:46,000
Now again I will be getting an complete empty dictionary okay.

46
00:02:46,000 --> 00:02:48,000
So right now it is a complete empty dictionary.

47
00:02:48,000 --> 00:02:55,000
So these are the two ways of specifically creating an empty dictionary Now, as I have already said,

48
00:02:55,000 --> 00:02:58,000
that dictionary is all about key value pairs.

49
00:02:58,000 --> 00:03:02,000
Right now, whenever we talk about key value pairs, let me just give you an example.

50
00:03:02,000 --> 00:03:03,000
So key.

51
00:03:03,000 --> 00:03:07,000
So initially we will you start writing in this flower brackets.

52
00:03:07,000 --> 00:03:12,000
So first of all what we are going to do is that we are going to give my key name, let's say one of

53
00:03:12,000 --> 00:03:15,000
the key name with respect to student information is name.

54
00:03:15,000 --> 00:03:18,000
And let me just go ahead and write the name called as Krish.

55
00:03:18,000 --> 00:03:18,000
Okay.

56
00:03:18,000 --> 00:03:20,000
So Krish is the name.

57
00:03:20,000 --> 00:03:21,000
So this basically becomes the key.

58
00:03:21,000 --> 00:03:23,000
This basically becomes the value.

59
00:03:23,000 --> 00:03:23,000
Okay.

60
00:03:24,000 --> 00:03:28,000
Now the next thing is that I will go ahead and use my another key which is called as age.

61
00:03:29,000 --> 00:03:30,000
Okay.

62
00:03:30,000 --> 00:03:34,000
And here we are specifically going to use a value let's say 32 okay.

63
00:03:34,000 --> 00:03:39,000
Third I'm just going to use another key which is called as grade.

64
00:03:39,000 --> 00:03:45,000
Now if I go ahead and write this you'll be able to see if I go ahead and print this student.

65
00:03:45,000 --> 00:03:52,000
This is a complete dictionary where I have three keys, that is name, age and grade, and the values

66
00:03:52,000 --> 00:03:55,000
are crush 32 and 24.

67
00:03:56,000 --> 00:03:57,000
Now if I go ahead and print the student.

68
00:03:57,000 --> 00:04:01,000
So this basically becomes my entire dictionary right?

69
00:04:01,000 --> 00:04:02,000
Which has this key value pairs.

70
00:04:02,000 --> 00:04:09,000
If you want to also check out the print off type of student, you will also be able to see that I'm

71
00:04:09,000 --> 00:04:11,000
able to get a dictionary over here.

72
00:04:11,000 --> 00:04:17,000
Okay, so this is how you specifically create a dictionary with the help of key value pairs.

73
00:04:17,000 --> 00:04:18,000
Okay.

74
00:04:18,000 --> 00:04:21,000
Now let me just show you one more thing here.

75
00:04:21,000 --> 00:04:22,000
When do you get an error.

76
00:04:22,000 --> 00:04:26,000
Because as I said you that key should be unique okay.

77
00:04:26,000 --> 00:04:31,000
So let's say if I go ahead and copy this and paste it over here and again I use the same name.

78
00:04:31,000 --> 00:04:33,000
Now what is going to happen.

79
00:04:33,000 --> 00:04:33,000
Okay.

80
00:04:33,000 --> 00:04:36,000
And I'll just go ahead and print student.

81
00:04:36,000 --> 00:04:40,000
Let's see what is going to happen.

82
00:04:40,000 --> 00:04:45,000
Because you really need to understand whether we will get an error or whether it will just print out.

83
00:04:45,000 --> 00:04:49,000
So here you can see only two keys are basically getting their name and age right.

84
00:04:49,000 --> 00:04:51,000
Name is having the value of 24.

85
00:04:51,000 --> 00:04:52,000
Age is 32.

86
00:04:52,000 --> 00:04:54,000
Now what has exactly happened over here?

87
00:04:54,000 --> 00:04:57,000
Now when I'm writing name is equal to Krish.

88
00:04:57,000 --> 00:04:59,000
This was one of the key that I have already defined.

89
00:04:59,000 --> 00:05:05,000
Now, if I'm again defining this particular key, you know, in the upcoming like or in the key value

90
00:05:05,000 --> 00:05:09,000
pairs, then what it is going to do, the value is getting going to get replaced.

91
00:05:09,000 --> 00:05:10,000
Okay.

92
00:05:10,000 --> 00:05:13,000
But it is going to consider as one key itself.

93
00:05:13,000 --> 00:05:18,000
So which is the recent value that you have replaced over here, the value that is what is getting displayed.

94
00:05:18,000 --> 00:05:24,000
So always please make sure that here you don't have to always create the key with the same name.

95
00:05:24,000 --> 00:05:29,000
I'll not say this as an error, but here you can see that, uh, I can just write.

96
00:05:29,000 --> 00:05:31,000
Single key is always used okay.

97
00:05:32,000 --> 00:05:33,000
Instead of multiple keys.

98
00:05:33,000 --> 00:05:35,000
So this is one super example.

99
00:05:35,000 --> 00:05:41,000
And this will be very much important because see, um, if you know about databases now NoSQL databases

100
00:05:41,000 --> 00:05:47,000
like MongoDB, like Cassandra over there, all key value pairs will be specifically used, right?

101
00:05:47,000 --> 00:05:49,000
All your data will be stored in key value pairs.

102
00:05:49,000 --> 00:05:53,000
So it is important that we always need to have this key unique.

103
00:05:53,000 --> 00:06:02,000
Right now let me just go ahead and let me talk about accessing dictionary elements okay.

104
00:06:02,000 --> 00:06:05,000
Now with respect to accessing dictionary elements.

105
00:06:05,000 --> 00:06:12,000
Now what I'm actually going to do, let me just go ahead and make this as, uh, I'll just copy this.

106
00:06:13,000 --> 00:06:13,000
Okay.

107
00:06:13,000 --> 00:06:16,000
And let me copy this over here.

108
00:06:17,000 --> 00:06:17,000
Oops.

109
00:06:17,000 --> 00:06:18,000
Not here.

110
00:06:18,000 --> 00:06:18,000
Sorry.

111
00:06:18,000 --> 00:06:19,000
Here.

112
00:06:19,000 --> 00:06:21,000
And let me just make this grade as a.

113
00:06:22,000 --> 00:06:22,000
Okay.

114
00:06:23,000 --> 00:06:27,000
Now, this is my entire dictionary, right?

115
00:06:27,000 --> 00:06:32,000
Now, if I want to access any value, let's say I want to access the value of grade.

116
00:06:32,000 --> 00:06:32,000
Okay.

117
00:06:32,000 --> 00:06:34,000
Now how do I do it?

118
00:06:34,000 --> 00:06:36,000
We definitely have to use this key.

119
00:06:36,000 --> 00:06:44,000
So what I will be doing is that I will just say student of I want the grade of this particular student.

120
00:06:44,000 --> 00:06:49,000
So I will just give the key name over here and let me just go ahead and print it.

121
00:06:49,000 --> 00:06:49,000
Okay.

122
00:06:49,000 --> 00:06:55,000
So here, once I do the print over here right, I should be able to get to see the grade.

123
00:06:55,000 --> 00:06:58,000
Similarly, let's say I want to go ahead and get the age.

124
00:06:59,000 --> 00:07:06,000
So I will go ahead and write student of student of age.

125
00:07:07,000 --> 00:07:07,000
Right.

126
00:07:07,000 --> 00:07:10,000
And if I go ahead and print it it is 32.

127
00:07:10,000 --> 00:07:10,000
Right.

128
00:07:10,000 --> 00:07:20,000
So this is one of the method of accessing I'll write it down over here accessing dictionary elements.

129
00:07:20,000 --> 00:07:20,000
Okay.

130
00:07:20,000 --> 00:07:24,000
This is one of the easiest way to probably do it.

131
00:07:24,000 --> 00:07:33,000
There is also one way where we will be able to access the values using the default get method that is

132
00:07:33,000 --> 00:07:35,000
provided by the dictionary.

133
00:07:35,000 --> 00:07:38,000
Okay, get method that is provided by dictionary.

134
00:07:38,000 --> 00:07:40,000
Now how to do this, I will just show you.

135
00:07:40,000 --> 00:07:44,000
Okay, so first of all I will just go ahead and write Print.

136
00:07:44,000 --> 00:07:47,000
Let's say I want to get the grade right.

137
00:07:47,000 --> 00:07:49,000
What is the grade of this particular student.

138
00:07:49,000 --> 00:07:51,000
So I will just go ahead and use Dot get.

139
00:07:51,000 --> 00:07:55,000
And here I'm just going to give my key name that is dot grade okay.

140
00:07:55,000 --> 00:08:04,000
Similarly let's say if I want to go ahead and print student dot and uh, let's say I want to probably,

141
00:08:05,000 --> 00:08:10,000
um, you know, let's say that there is no, like over here.

142
00:08:10,000 --> 00:08:13,000
The key that are available are name, age, grade.

143
00:08:13,000 --> 00:08:13,000
Right.

144
00:08:13,000 --> 00:08:17,000
Let's say I will go ahead and search for last name.

145
00:08:17,000 --> 00:08:17,000
Okay.

146
00:08:17,000 --> 00:08:19,000
I know the key is not present over there.

147
00:08:19,000 --> 00:08:23,000
So let's see what is the value that I will be getting okay.

148
00:08:23,000 --> 00:08:26,000
And by using get we will be able to get the value anyhow.

149
00:08:26,000 --> 00:08:30,000
But if I execute it here you'll be able to see first student grade.

150
00:08:30,000 --> 00:08:32,000
I got a student age.

151
00:08:32,000 --> 00:08:33,000
I got 32 again.

152
00:08:33,000 --> 00:08:37,000
When I'm writing student dot get of grade, I can get a.

153
00:08:37,000 --> 00:08:42,000
But when I'm searching for last name, I'm not getting anything right.

154
00:08:42,000 --> 00:08:43,000
I'm getting it as none.

155
00:08:43,000 --> 00:08:49,000
Now let's say that, uh, if I find out if I don't find any key, I want to probably give a default

156
00:08:49,000 --> 00:08:49,000
value.

157
00:08:49,000 --> 00:08:56,000
So for that I will go ahead and write print student student Dot.

158
00:08:56,000 --> 00:08:57,000
Get.

159
00:08:57,000 --> 00:08:57,000
Okay.

160
00:08:58,000 --> 00:09:04,000
And here I'm going to specifically write last underscore name I know last number underscore name key

161
00:09:04,000 --> 00:09:05,000
is not available.

162
00:09:05,000 --> 00:09:08,000
But I will give a default value if it is not available.

163
00:09:08,000 --> 00:09:10,000
So I'll write not available.

164
00:09:10,000 --> 00:09:13,000
Now let's see the value that we are going to get.

165
00:09:13,000 --> 00:09:14,000
So see over here.

166
00:09:14,000 --> 00:09:16,000
Last name was not present in this particular key.

167
00:09:16,000 --> 00:09:20,000
So I am getting the default value which I have provided it over here as not available.

168
00:09:20,000 --> 00:09:22,000
So before it was giving us none.

169
00:09:22,000 --> 00:09:25,000
Now it is probably giving us not available.

170
00:09:25,000 --> 00:09:25,000
Right?

171
00:09:25,000 --> 00:09:30,000
So this is how with the help of key, you will be able to access it.

172
00:09:30,000 --> 00:09:34,000
And two ways I have actually shown you how to access dictionary elements.

173
00:09:34,000 --> 00:09:39,000
Either you are directly calling the key or you are using the Get method right.

174
00:09:39,000 --> 00:09:48,000
Now let me just go ahead and let me talk about the next method that is called as modifying dictionary

175
00:09:49,000 --> 00:09:50,000
elements.

176
00:09:50,000 --> 00:09:50,000
Okay.

177
00:09:50,000 --> 00:09:57,000
Now with respect to modifying dictionary elements, one very important key that I really want to write,

178
00:09:57,000 --> 00:09:57,000
right?

179
00:09:57,000 --> 00:10:00,000
Dictionary are mutable right?

180
00:10:00,000 --> 00:10:08,000
So you can add, update, update or delete elements.

181
00:10:08,000 --> 00:10:12,000
Okay, so this is the most important thing about dictionary.

182
00:10:12,000 --> 00:10:13,000
But you cannot see.

183
00:10:13,000 --> 00:10:16,000
You cannot like always all the time.

184
00:10:16,000 --> 00:10:19,000
Your key needs to be unique right at any point of time.

185
00:10:19,000 --> 00:10:23,000
So let me just go ahead and show you some of the examples over here.

186
00:10:23,000 --> 00:10:27,000
So let us go ahead and discuss about modifying dictionary elements.

187
00:10:27,000 --> 00:10:33,000
Uh, so here uh, already, uh, let us go ahead and print this students.

188
00:10:33,000 --> 00:10:34,000
Right.

189
00:10:35,000 --> 00:10:38,000
And, uh, here you can see the name is the key.

190
00:10:38,000 --> 00:10:43,000
The key name, uh, is basically having the value crush age is 32.

191
00:10:43,000 --> 00:10:44,000
Grade is a.

192
00:10:44,000 --> 00:10:46,000
Now let me do one thing.

193
00:10:46,000 --> 00:10:49,000
Let me just go ahead and, uh, add a new key.

194
00:10:49,000 --> 00:10:53,000
I'll also be showing to add a new key and also updating let's say age.

195
00:10:53,000 --> 00:11:01,000
So here now if I go ahead and write student of age and let's say the age is now 33.

196
00:11:01,000 --> 00:11:01,000
Okay.

197
00:11:01,000 --> 00:11:09,000
And uh, I'm just going to print the student now along with that, what I am actually going to do is

198
00:11:09,000 --> 00:11:11,000
that I'm going to add a new key.

199
00:11:11,000 --> 00:11:14,000
And the new key will be something like address.

200
00:11:15,000 --> 00:11:16,000
Okay.

201
00:11:16,000 --> 00:11:20,000
And let me just go ahead and write the address as India.

202
00:11:20,000 --> 00:11:20,000
Okay.

203
00:11:21,000 --> 00:11:23,000
So this is the new key that I'm specifically adding.

204
00:11:23,000 --> 00:11:27,000
Now I will go ahead and print the student.

205
00:11:29,000 --> 00:11:35,000
Now here you can actually see that the first key age has got updated to 33.

206
00:11:35,000 --> 00:11:41,000
Now once it got updated I again added a new key over here which is called as address.

207
00:11:41,000 --> 00:11:47,000
So now I will be able to see the address India that has been added to the same, uh, student dictionary.

208
00:11:47,000 --> 00:11:48,000
Okay.

209
00:11:48,000 --> 00:11:51,000
Along with this, uh, we can also go ahead and delete any key.

210
00:11:51,000 --> 00:11:52,000
Right.

211
00:11:52,000 --> 00:11:56,000
So what I'm actually going to do is that I'm going to delete the key over here.

212
00:11:56,000 --> 00:11:57,000
Okay.

213
00:11:57,000 --> 00:11:59,000
Let's consider that I'm going to delete the grade key.

214
00:11:59,000 --> 00:12:01,000
So let's go ahead and see that.

215
00:12:02,000 --> 00:12:12,000
So here what we have actually done we have updated the value for the key Right here we have added a

216
00:12:12,000 --> 00:12:19,000
new key and value, added a new key and value.

217
00:12:21,000 --> 00:12:27,000
And in the next statement, what I'm actually going to do is that I'm going to delete the key.

218
00:12:27,000 --> 00:12:32,000
So in order to delete the key I just need to use this del keyword okay.

219
00:12:32,000 --> 00:12:36,000
That is available in Python and I will be using the student.

220
00:12:36,000 --> 00:12:38,000
And let me just give the key name.

221
00:12:38,000 --> 00:12:45,000
So if I go ahead and write like this, this will basically delete the key and value pair.

222
00:12:45,000 --> 00:12:46,000
Okay.

223
00:12:46,000 --> 00:12:56,000
Now if I go ahead and print the student, you will be able to see that I will now have the entire key

224
00:12:56,000 --> 00:12:57,000
right?

225
00:12:57,000 --> 00:13:03,000
Grade has got deleted along with the value, and I just have the remaining key that is key value pairs

226
00:13:03,000 --> 00:13:06,000
like name crush, age 33 address India.

227
00:13:06,000 --> 00:13:09,000
Okay, so this is how you delete the key value pair.

228
00:13:09,000 --> 00:13:13,000
So these are some of the operations and how we can modify dictionary elements.

229
00:13:13,000 --> 00:13:13,000
Okay.

230
00:13:14,000 --> 00:13:21,000
Um now let me just go ahead and show you some of the dictionary common dictionary methods that we use.

231
00:13:21,000 --> 00:13:22,000
Okay.

232
00:13:23,000 --> 00:13:29,000
Now you'll be also seeing more examples because after this I will be also showing showing some use cases

233
00:13:29,000 --> 00:13:30,000
examples okay.

234
00:13:30,000 --> 00:13:33,000
So there again we will be discussing more about dictionaries okay.

235
00:13:33,000 --> 00:13:38,000
Now some of the important dictionary methods okay.

236
00:13:38,000 --> 00:13:39,000
So first of all let me just go ahead.

237
00:13:39,000 --> 00:13:44,000
Let's say you just want to get the all the keys of that particular dictionary okay.

238
00:13:44,000 --> 00:13:54,000
So I will go ahead and write keys is equal to student which is my dictionary student dot keys.

239
00:13:54,000 --> 00:13:55,000
Okay.

240
00:13:55,000 --> 00:13:59,000
And let me just go ahead and print all the keys over here.

241
00:13:59,000 --> 00:14:02,000
Okay then let me just go ahead and write.

242
00:14:02,000 --> 00:14:06,000
Values is equal to student dot values.

243
00:14:07,000 --> 00:14:07,000
Okay.

244
00:14:07,000 --> 00:14:14,000
If I want to get the values right off all the dictionary that I have basically in the dictionary, all

245
00:14:14,000 --> 00:14:17,000
the values that I if I want all the values, how can I get it?

246
00:14:17,000 --> 00:14:22,000
So this first code that I've actually written, this will actually help you to get all the keys.

247
00:14:23,000 --> 00:14:23,000
Okay.

248
00:14:23,000 --> 00:14:28,000
And this will actually help you to get all the values okay.

249
00:14:28,000 --> 00:14:33,000
And if I go ahead and print the values I will be able to see the values over here.

250
00:14:34,000 --> 00:14:35,000
Uh, one more.

251
00:14:35,000 --> 00:14:42,000
Uh, let's say I want to get all the key value pairs, let's say in the form of three items.

252
00:14:42,000 --> 00:14:42,000
Okay.

253
00:14:42,000 --> 00:14:46,000
How do I get the key value pairs right in that case.

254
00:14:46,000 --> 00:14:48,000
So let me just go ahead and write.

255
00:14:48,000 --> 00:14:50,000
And I'll define a variable called as items.

256
00:14:50,000 --> 00:14:55,000
And if I go ahead and write student dot items right.

257
00:14:55,000 --> 00:15:01,000
And this will actually help me to get all key value pairs.

258
00:15:01,000 --> 00:15:02,000
Okay.

259
00:15:02,000 --> 00:15:07,000
Now let me just go ahead and print this specific items.

260
00:15:07,000 --> 00:15:08,000
Okay.

261
00:15:08,000 --> 00:15:12,000
So here you can see that uh let's see the first one okay.

262
00:15:12,000 --> 00:15:14,000
Student Dot keys I'm getting all the key.

263
00:15:14,000 --> 00:15:16,000
So name, age and address are the key.

264
00:15:16,000 --> 00:15:18,000
When I wrote student dot values I got all the values.

265
00:15:18,000 --> 00:15:19,000
I'm printing it.

266
00:15:19,000 --> 00:15:21,000
So Krish 33 and India.

267
00:15:21,000 --> 00:15:24,000
And when I want all the key value pairs.

268
00:15:24,000 --> 00:15:26,000
So like this I'm able to get it right.

269
00:15:26,000 --> 00:15:31,000
So this will be a new type which is called as dictionary items which will be in the form of list of

270
00:15:31,000 --> 00:15:31,000
tuples.

271
00:15:31,000 --> 00:15:33,000
So these are nothing but tuples right.

272
00:15:33,000 --> 00:15:35,000
Tuples tuples tuples.

273
00:15:35,000 --> 00:15:37,000
Right I will be able to access this particular tuples also.

274
00:15:37,000 --> 00:15:39,000
And it is a list of tuples okay.

275
00:15:39,000 --> 00:15:44,000
So here you can see name Chris age 33 A address India.

276
00:15:44,000 --> 00:15:46,000
Right now one more thing.

277
00:15:46,000 --> 00:15:50,000
Uh, I really want to do is do a shallow copy.

278
00:15:50,000 --> 00:15:50,000
Okay.

279
00:15:50,000 --> 00:15:53,000
Now let us discuss about shallow copy over here.

280
00:15:53,000 --> 00:15:57,000
So, guys, now let me just go ahead and show you the working of shallow copy.

281
00:15:57,000 --> 00:15:59,000
And this is something really, really important.

282
00:15:59,000 --> 00:16:01,000
And trust me, you should never do this mistake.

283
00:16:01,000 --> 00:16:07,000
Whenever you are implementing in any kind of project, you have to make sure that whenever, uh.

284
00:16:07,000 --> 00:16:12,000
And you need to remember this concept because this can be a very small mistake whenever you're developing

285
00:16:12,000 --> 00:16:13,000
projects.

286
00:16:13,000 --> 00:16:19,000
Okay, now I have student dictionary over here right now.

287
00:16:19,000 --> 00:16:19,000
Let me do one thing.

288
00:16:19,000 --> 00:16:22,000
Let me create another variable.

289
00:16:22,000 --> 00:16:25,000
Let's say I will go ahead and write student underscore copy.

290
00:16:25,000 --> 00:16:27,000
And I will assign the student value over here.

291
00:16:27,000 --> 00:16:32,000
And as you all know what is present in the student the key value pairs.

292
00:16:32,000 --> 00:16:41,000
Right now if I go ahead and print student, and if I go ahead and print student underscore copy okay.

293
00:16:43,000 --> 00:16:45,000
Student underscore copy.

294
00:16:45,000 --> 00:16:47,000
What am I getting?

295
00:16:47,000 --> 00:16:48,000
I'm getting the same value.

296
00:16:48,000 --> 00:16:49,000
This is perfectly fine.

297
00:16:49,000 --> 00:16:54,000
And obviously since I'm copying this particular value to another variable.

298
00:16:54,000 --> 00:16:57,000
So this will also be a dictionary that will be having the same value over here okay.

299
00:16:57,000 --> 00:16:57,000
Okay.

300
00:16:58,000 --> 00:17:04,000
But now what I will do I will just go ahead and take student name value.

301
00:17:05,000 --> 00:17:05,000
Right.

302
00:17:05,000 --> 00:17:08,000
Student name value.

303
00:17:08,000 --> 00:17:11,000
And I will assign some other value itself.

304
00:17:11,000 --> 00:17:11,000
Right.

305
00:17:11,000 --> 00:17:13,000
Let's say crush one okay.

306
00:17:13,000 --> 00:17:19,000
Now let me just go ahead and print student and let me print the same student underscore copy.

307
00:17:20,000 --> 00:17:25,000
What do you think should be the name in student underscore copy?

308
00:17:26,000 --> 00:17:28,000
Should it be Christian or Christian?

309
00:17:28,000 --> 00:17:29,000
Okay, I'll make it to Christian.

310
00:17:29,000 --> 00:17:30,000
Okay.

311
00:17:30,000 --> 00:17:35,000
Let's say I'm changing the name of this particular student right over here to Cristo.

312
00:17:36,000 --> 00:17:41,000
Now, since I'm making the change to this student dictionary, since I made a copy of this in student

313
00:17:41,000 --> 00:17:46,000
underscore copy, then you may be thinking, okay, student underscore copy should still have Christian,

314
00:17:46,000 --> 00:17:47,000
but it does not happen in that way.

315
00:17:47,000 --> 00:17:52,000
If I go ahead and see the output, you'll be seeing that since student also got the name as Krish two

316
00:17:52,000 --> 00:17:55,000
since we had assigned the value to student underscore copy.

317
00:17:55,000 --> 00:17:57,000
So here also we are getting Krish two.

318
00:17:57,000 --> 00:17:59,000
And this is a big problem right?

319
00:17:59,000 --> 00:18:02,000
So how do I make a separate copy.

320
00:18:02,000 --> 00:18:07,000
You know whenever I probably want to make a copy for this particular dictionary to an another variable

321
00:18:07,000 --> 00:18:13,000
in such a way that after that if I update this, the same update should not happen in your copy variable.

322
00:18:13,000 --> 00:18:17,000
So for that I will be showing you about shallow copy.

323
00:18:17,000 --> 00:18:23,000
Now in shallow copy, what we do is that let's say I go ahead and create my student copy one variable.

324
00:18:23,000 --> 00:18:26,000
I will go ahead and write student dot copy function.

325
00:18:26,000 --> 00:18:26,000
Okay.

326
00:18:26,000 --> 00:18:30,000
Now this copy function is responsible for doing the shallow copy over here.

327
00:18:30,000 --> 00:18:35,000
In short if you really want to understand this will basically allocate a different memory.

328
00:18:35,000 --> 00:18:40,000
It will take this variable and it will give the reference of this variable to an another memory, which

329
00:18:40,000 --> 00:18:43,000
will have that entire value that we really want to store.

330
00:18:44,000 --> 00:18:44,000
Okay.

331
00:18:44,000 --> 00:18:53,000
Now if I go ahead and print student underscore copy student underscore copy one okay.

332
00:18:53,000 --> 00:18:57,000
And if I go want to go ahead and print student.

333
00:18:57,000 --> 00:18:58,000
Right.

334
00:18:58,000 --> 00:19:01,000
And if I execute it now here you can see, okay, I'm having the same value.

335
00:19:01,000 --> 00:19:08,000
But now let me just update the student name, okay.

336
00:19:08,000 --> 00:19:13,000
If I go ahead and update the student name, let's say if I go ahead and write Chris three okay.

337
00:19:14,000 --> 00:19:19,000
Now if I go ahead and print both of them, will I get the same value of the name?

338
00:19:19,000 --> 00:19:20,000
The answer is no.

339
00:19:20,000 --> 00:19:25,000
See, I will execute this now in my student underscore copy one.

340
00:19:25,000 --> 00:19:29,000
I still have Chris two and student I have Chris three.

341
00:19:29,000 --> 00:19:29,000
Right.

342
00:19:29,000 --> 00:19:33,000
And why this was possible because of the shallow copy itself.

343
00:19:33,000 --> 00:19:33,000
Right.

344
00:19:33,000 --> 00:19:35,000
So just get an idea about shallow copy.

345
00:19:35,000 --> 00:19:42,000
Uh, shallow copy in turns, you know, provides a different memory location so that whenever we change

346
00:19:42,000 --> 00:19:45,000
one of the variable, it should not impact the other one.

347
00:19:45,000 --> 00:19:45,000
Okay.

348
00:19:45,000 --> 00:19:50,000
So just a smaller thing to understand over here because this will be important when you are doing the

349
00:19:50,000 --> 00:19:51,000
coding.

350
00:19:51,000 --> 00:19:58,000
Now let's go ahead and discuss about iterating over dictionaries.

351
00:19:58,000 --> 00:19:59,000
Okay.

352
00:19:59,000 --> 00:20:04,000
First thing you can use loops to iterate.

353
00:20:04,000 --> 00:20:11,000
Like how did we do in the in the in the case of list or in the case of tuples.

354
00:20:11,000 --> 00:20:14,000
So here also we are specifically.

355
00:20:15,000 --> 00:20:16,000
Okay I'll write.

356
00:20:16,000 --> 00:20:22,000
You can use okay loops to iterate.

357
00:20:23,000 --> 00:20:26,000
And we can iterate over dictionaries.

358
00:20:26,000 --> 00:20:28,000
And you know in dictionaries.

359
00:20:28,000 --> 00:20:34,000
Uh, if I probably talk about we have keys, we have values or we have items.

360
00:20:34,000 --> 00:20:38,000
Items basically means the combination of key and value pair, right.

361
00:20:38,000 --> 00:20:40,000
So let me just go ahead and show you the same example.

362
00:20:40,000 --> 00:20:43,000
Let's say that this is my student variable okay.

363
00:20:43,000 --> 00:20:47,000
And for this particular student I will go ahead and iterate through each and everything.

364
00:20:47,000 --> 00:20:53,000
So first of all I will try to show you iterating over keys okay.

365
00:20:53,000 --> 00:20:59,000
Now iterating over keys for keys in again we'll use a for loop and I will just go ahead and write student

366
00:20:59,000 --> 00:21:00,000
dot keys.

367
00:21:00,000 --> 00:21:04,000
When I write student dot keys, obviously you'll be able to see that I'll be getting the keys itself.

368
00:21:04,000 --> 00:21:08,000
If I go ahead and print the key, you'll be able to see.

369
00:21:09,000 --> 00:21:09,000
Oops.

370
00:21:10,000 --> 00:21:10,000
Uh, keys.

371
00:21:10,000 --> 00:21:11,000
Sorry.

372
00:21:11,000 --> 00:21:14,000
You'll be able to see name, age, and address.

373
00:21:14,000 --> 00:21:16,000
These are the keys that I have actually defined.

374
00:21:16,000 --> 00:21:28,000
Similarly, you can iterate iterate over values, and for that what you need to do again, copy the

375
00:21:28,000 --> 00:21:28,000
same thing.

376
00:21:29,000 --> 00:21:29,000
Right?

377
00:21:29,000 --> 00:21:32,000
Paste it over here instead of student dot keys.

378
00:21:32,000 --> 00:21:35,000
Now you go ahead and write student dot values.

379
00:21:36,000 --> 00:21:37,000
Okay.

380
00:21:37,000 --> 00:21:43,000
And once I go ahead and write it over here I can print it over here itself.

381
00:21:43,000 --> 00:21:46,000
And this is what you will be able to get a value Chris three.

382
00:21:46,000 --> 00:21:49,000
You'll be able to get 33 India.

383
00:21:49,000 --> 00:22:05,000
Okay, now if I want to iterate, iterate over key value pairs, then what we really need to do I will

384
00:22:05,000 --> 00:22:08,000
be using two variable in my for loop.

385
00:22:08,000 --> 00:22:15,000
One is key, comma value and student dot items.

386
00:22:16,000 --> 00:22:21,000
And now I will just go ahead and print by using an f string.

387
00:22:21,000 --> 00:22:22,000
Okay.

388
00:22:22,000 --> 00:22:25,000
So this is one way of formatting.

389
00:22:25,000 --> 00:22:28,000
Um in sprint formatting I can go ahead and write my key.

390
00:22:29,000 --> 00:22:30,000
Along with this.

391
00:22:30,000 --> 00:22:34,000
I can go ahead and write my value if I go ahead and see this.

392
00:22:34,000 --> 00:22:35,000
This is my age.

393
00:22:35,000 --> 00:22:37,000
Three age.

394
00:22:37,000 --> 00:22:37,000
Uh, sorry.

395
00:22:37,000 --> 00:22:39,000
Name Chris three, age 33.

396
00:22:39,000 --> 00:22:40,000
Address India.

397
00:22:41,000 --> 00:22:44,000
So you can also iterate over the key value pairs.

398
00:22:44,000 --> 00:22:49,000
And just by writing string dot items, you get that value in tuples right in list of tuples.

399
00:22:49,000 --> 00:22:54,000
So all these things you can basically work and you can basically iterate over the dictionaries.

400
00:22:54,000 --> 00:23:01,000
Now talking about the next one, uh, here we are going to go ahead with let me make some cells.

401
00:23:01,000 --> 00:23:01,000
Okay.

402
00:23:01,000 --> 00:23:07,000
So we are going to talk about nested dictionaries.

403
00:23:07,000 --> 00:23:07,000
Okay.

404
00:23:08,000 --> 00:23:09,000
Now nested dictionaries.

405
00:23:09,000 --> 00:23:12,000
A dictionary inside another dictionary okay.

406
00:23:12,000 --> 00:23:15,000
And usually if I talk about databases MongoDB.

407
00:23:15,000 --> 00:23:16,000
Right.

408
00:23:16,000 --> 00:23:17,000
NoSQL database over there.

409
00:23:17,000 --> 00:23:19,000
Everything is stored in key value pair.

410
00:23:19,000 --> 00:23:23,000
Whenever we say key value pair later on we can read that convert that into a dictionary.

411
00:23:23,000 --> 00:23:26,000
And I may have any number of records in the MongoDB database.

412
00:23:26,000 --> 00:23:27,000
Right.

413
00:23:27,000 --> 00:23:29,000
So let me just go ahead and do one thing.

414
00:23:29,000 --> 00:23:33,000
Let me go ahead and write students, and let me just show you how you can create key value pairs.

415
00:23:33,000 --> 00:23:34,000
Okay.

416
00:23:35,000 --> 00:23:37,000
So let's say this is my first key.

417
00:23:37,000 --> 00:23:39,000
That is nothing but student one.

418
00:23:39,000 --> 00:23:42,000
And I will just go ahead and write my key value pair here.

419
00:23:42,000 --> 00:23:47,000
The first thing I will go and see name name is let's say Krish.

420
00:23:48,000 --> 00:23:50,000
Uh, age is.

421
00:23:50,000 --> 00:23:54,000
Let's say colon 32.

422
00:23:54,000 --> 00:23:55,000
Okay.

423
00:23:55,000 --> 00:23:57,000
And, uh, age 32.

424
00:23:57,000 --> 00:24:00,000
And here I am specifically creating this.

425
00:24:00,000 --> 00:24:00,000
Right.

426
00:24:00,000 --> 00:24:01,000
Sorry.

427
00:24:01,000 --> 00:24:05,000
It should be colon not equal to colon 32.

428
00:24:05,000 --> 00:24:05,000
Okay.

429
00:24:06,000 --> 00:24:07,000
This is my first record.

430
00:24:07,000 --> 00:24:08,000
Huh.

431
00:24:08,000 --> 00:24:11,000
Now let me go ahead and create my second record.

432
00:24:11,000 --> 00:24:17,000
Let's say I may have student two data And now with respect to this particular student two data, I may

433
00:24:17,000 --> 00:24:18,000
have name.

434
00:24:19,000 --> 00:24:32,000
Name is nothing but let's say fresh or I'll just say, um, Spider-Man or Peter Parker or anyone.

435
00:24:32,000 --> 00:24:32,000
Right?

436
00:24:32,000 --> 00:24:33,000
So Peter.

437
00:24:33,000 --> 00:24:34,000
Right.

438
00:24:35,000 --> 00:24:38,000
Age is nothing but let's say 35.

439
00:24:38,000 --> 00:24:39,000
Right.

440
00:24:39,000 --> 00:24:41,000
So here you can see key value pairs.

441
00:24:41,000 --> 00:24:42,000
Right.

442
00:24:42,000 --> 00:24:43,000
And this is nothing.

443
00:24:43,000 --> 00:24:45,000
But this is basically a nested dictionary.

444
00:24:45,000 --> 00:24:53,000
Right now if I go ahead and print students you will be able to see that hey it looks like a dictionary.

445
00:24:53,000 --> 00:24:54,000
Key value pairs.

446
00:24:54,000 --> 00:24:56,000
Again it can have nested key value pairs.

447
00:24:56,000 --> 00:24:57,000
Right.

448
00:24:57,000 --> 00:25:04,000
But these are just like a kind of records like how we uploaded in MongoDB or in any other NoSQL database,

449
00:25:04,000 --> 00:25:06,000
NoSQL databases right now.

450
00:25:06,000 --> 00:25:08,000
How do we access this?

451
00:25:08,000 --> 00:25:16,000
Nested dictionaries access nested dictionaries, dictionaries, elements.

452
00:25:16,000 --> 00:25:17,000
Okay.

453
00:25:17,000 --> 00:25:22,000
So I will just go ahead and print right students.

454
00:25:24,000 --> 00:25:29,000
And here I'm going to use, let's say I want to read The Age of Peter Parker.

455
00:25:29,000 --> 00:25:30,000
Then how do I write?

456
00:25:30,000 --> 00:25:31,000
So I will just go ahead and write.

457
00:25:31,000 --> 00:25:32,000
Student uh.

458
00:25:32,000 --> 00:25:34,000
Student two okay.

459
00:25:34,000 --> 00:25:39,000
And the next bracket will be specifically name okay or name or age.

460
00:25:39,000 --> 00:25:43,000
I can also get name if I want to print, I can print name.

461
00:25:43,000 --> 00:25:48,000
Let's say if I go ahead and print this entire information along with age, here I will be getting age

462
00:25:48,000 --> 00:25:49,000
or name of Peter Parker.

463
00:25:49,000 --> 00:25:52,000
And and here I'll be getting the age right.

464
00:25:52,000 --> 00:25:55,000
So if I go and see Peter and 35 right.

465
00:25:55,000 --> 00:26:00,000
So here with respect to the nested dictionaries, also like how we read in in nested list.

466
00:26:00,000 --> 00:26:02,000
Similarly we can go ahead and do that here.

467
00:26:02,000 --> 00:26:06,000
The main thing is that we only have to call the key names, and automatically you'll be able to get

468
00:26:06,000 --> 00:26:08,000
the value names right.

469
00:26:08,000 --> 00:26:11,000
So this is one amazing way of playing with nested dictionaries.

470
00:26:11,000 --> 00:26:12,000
Okay.

471
00:26:12,000 --> 00:26:15,000
Now uh, again, you can try multiple example.

472
00:26:15,000 --> 00:26:19,000
You can create your own nested dictionaries, any level of nested dictionaries.

473
00:26:20,000 --> 00:26:22,000
Uh, there is no limit as such inside name.

474
00:26:22,000 --> 00:26:25,000
Also, you can again go ahead and create another nested dictionary if you want.

475
00:26:25,000 --> 00:26:26,000
Okay.

476
00:26:26,000 --> 00:26:34,000
Now let me just go ahead and show you one more thing iterating over nested dictionaries.

477
00:26:34,000 --> 00:26:34,000
Okay.

478
00:26:35,000 --> 00:26:38,000
Now for let's say I will go ahead and write for student underscore ID.

479
00:26:39,000 --> 00:26:39,000
Okay.

480
00:26:39,000 --> 00:26:42,000
Since I require ID and information.

481
00:26:42,000 --> 00:26:44,000
So let's say I will go ahead and write like this.

482
00:26:44,000 --> 00:26:46,000
Just see this okay.

483
00:26:46,000 --> 00:26:53,000
Student underscore info okay I will consider this as ID and this as the entire info okay.

484
00:26:53,000 --> 00:27:01,000
And if I go ahead and write students students students okay Okay.

485
00:27:01,000 --> 00:27:03,000
Dot items.

486
00:27:04,000 --> 00:27:09,000
So what will student dot item give me if I go ahead and execute a code over here?

487
00:27:09,000 --> 00:27:14,000
What will students dot items items give me.

488
00:27:14,000 --> 00:27:17,000
It will give me dictionary in the key value pairs.

489
00:27:17,000 --> 00:27:18,000
Right.

490
00:27:18,000 --> 00:27:23,000
Like student one will be one key and this will be a entire value right inside this.

491
00:27:23,000 --> 00:27:27,000
This can be a key and this can be a value, but it will be giving you in this particular format.

492
00:27:27,000 --> 00:27:32,000
Now if I iterate over this nested dictionary, this I will consider as my id, and this will I'll consider

493
00:27:32,000 --> 00:27:33,000
as my value.

494
00:27:33,000 --> 00:27:34,000
Right.

495
00:27:34,000 --> 00:27:38,000
So that is the reason I've written from student underscore ID and student underscore info.

496
00:27:38,000 --> 00:27:38,000
Right.

497
00:27:38,000 --> 00:27:40,000
So this can be a temporary variable.

498
00:27:40,000 --> 00:27:43,000
Right now I will just go ahead and print okay.

499
00:27:43,000 --> 00:27:46,000
So one print I will just use an F string.

500
00:27:46,000 --> 00:27:51,000
And inside this f string let me first of all go ahead and display my student underscore ID okay.

501
00:27:51,000 --> 00:27:56,000
And this will basically be my student underscore info.

502
00:27:56,000 --> 00:28:04,000
Right now I may want a scenario wherein I traverse through or I iterate over all the elements that are

503
00:28:04,000 --> 00:28:05,000
inside this.

504
00:28:05,000 --> 00:28:05,000
Right.

505
00:28:05,000 --> 00:28:06,000
So let me do that.

506
00:28:06,000 --> 00:28:10,000
So I will go ahead and write for key comma value okay.

507
00:28:10,000 --> 00:28:16,000
In student underscore info right I need to iterate through this.

508
00:28:16,000 --> 00:28:19,000
And this all information are present inside student underscore info.

509
00:28:19,000 --> 00:28:22,000
Now inside this also I will be able to find out keys and values.

510
00:28:22,000 --> 00:28:27,000
So I will go ahead and print F again my key.

511
00:28:28,000 --> 00:28:35,000
And this will be nothing but it will be colon again my value right now let's go ahead and see.

512
00:28:35,000 --> 00:28:42,000
Uh so there is an error for key comma value in student underscore info dot items.

513
00:28:42,000 --> 00:28:47,000
I, I missed out dot items because I am not getting too many information out of it.

514
00:28:47,000 --> 00:28:49,000
So see how it is printing this first information.

515
00:28:49,000 --> 00:28:51,000
This is my student.

516
00:28:51,000 --> 00:28:52,000
All the information is over here.

517
00:28:52,000 --> 00:28:56,000
Then I took this info and I iterated through every key and values.

518
00:28:56,000 --> 00:28:57,000
Right?

519
00:28:57,000 --> 00:28:58,000
So name is equal to crush.

520
00:28:58,000 --> 00:28:59,000
Age is equal to 32.

521
00:28:59,000 --> 00:29:01,000
Then again it went to the for loop.

522
00:29:01,000 --> 00:29:07,000
It found out the another student ID that is student two with this name and now it is printing name is

523
00:29:07,000 --> 00:29:08,000
equal to Peter and age is equal to five.

524
00:29:09,000 --> 00:29:13,000
So this is how you iterate over nested dictionaries.

525
00:29:13,000 --> 00:29:16,000
And you can play with different different elements as you want.

526
00:29:16,000 --> 00:29:16,000
Okay.

527
00:29:17,000 --> 00:29:22,000
So again Ellen any level of nesting let it be right.

528
00:29:22,000 --> 00:29:23,000
You can iterate through each and everything.

529
00:29:23,000 --> 00:29:27,000
And just by using dot items you can get the key value pairs.

530
00:29:27,000 --> 00:29:35,000
Okay, now let me just go ahead and talk about dictionary comprehension.

531
00:29:35,000 --> 00:29:39,000
Like how discussed about nested list uh list comprehension.

532
00:29:39,000 --> 00:29:43,000
Similarly we'll discuss about uh dictionary comprehension okay.

533
00:29:43,000 --> 00:29:45,000
Now dictionary comprehension.

534
00:29:45,000 --> 00:29:46,000
How does it work.

535
00:29:46,000 --> 00:29:55,000
Uh, and uh, let's say that I want to just print what is the square of a specific number between 1

536
00:29:55,000 --> 00:29:56,000
to 10.

537
00:29:56,000 --> 00:29:56,000
Okay.

538
00:29:56,000 --> 00:29:58,000
So let me just go ahead and create a variable.

539
00:29:58,000 --> 00:30:00,000
Squares is equal to.

540
00:30:00,000 --> 00:30:03,000
And again what is the mechanism in list comprehension.

541
00:30:03,000 --> 00:30:05,000
We basically write like this right.

542
00:30:05,000 --> 00:30:10,000
In dictionary comprehension we use uh angular braces right.

543
00:30:10,000 --> 00:30:13,000
So first thing first always remember we need to write.

544
00:30:13,000 --> 00:30:14,000
You can write a for loop.

545
00:30:14,000 --> 00:30:15,000
You can write any condition over here.

546
00:30:15,000 --> 00:30:19,000
So let me go ahead and write for x in range of five okay.

547
00:30:20,000 --> 00:30:25,000
So I'm iterating between 0 to 5 and what I want I want to display something like this x is equal to

548
00:30:25,000 --> 00:30:30,000
x square okay I'm basically squaring each and every number.

549
00:30:30,000 --> 00:30:33,000
And that is what is my x value.

550
00:30:33,000 --> 00:30:36,000
And this way it will probably display me.

551
00:30:36,000 --> 00:30:38,000
And this is the way how key and value pairs displays.

552
00:30:38,000 --> 00:30:39,000
Right.

553
00:30:39,000 --> 00:30:39,000
right?

554
00:30:39,000 --> 00:30:40,000
So this is my key.

555
00:30:40,000 --> 00:30:41,000
This will be the value.

556
00:30:41,000 --> 00:30:43,000
This will be my key.

557
00:30:43,000 --> 00:30:45,000
This will be the value as we keep on iterating.

558
00:30:45,000 --> 00:30:52,000
So if I just go ahead and execute this print squares again you can one assignment I definitely like

559
00:30:52,000 --> 00:30:53,000
to give you.

560
00:30:53,000 --> 00:30:57,000
Just go ahead and write this entire thing with a simple for loop okay.

561
00:30:57,000 --> 00:31:01,000
And then you try to convert this into a dictionary right a dictionary comprehension.

562
00:31:01,000 --> 00:31:04,000
So here you can see now I'm able to get a key value pair.

563
00:31:04,000 --> 00:31:05,000
So this is my zero is my key.

564
00:31:05,000 --> 00:31:06,000
Zero is the value.

565
00:31:06,000 --> 00:31:07,000
One is the key one is the value.

566
00:31:07,000 --> 00:31:09,000
Two is the key, four is the value.

567
00:31:09,000 --> 00:31:11,000
And we are doing the squaring of it right.

568
00:31:11,000 --> 00:31:12,000
Squaring off each and every number.

569
00:31:14,000 --> 00:31:20,000
Now let's say I want to probably go ahead and just find out the squares off only even number.

570
00:31:20,000 --> 00:31:22,000
So here I will also show you by writing conditions.

571
00:31:22,000 --> 00:31:27,000
So let me just go ahead and write conditional dictionary.

572
00:31:29,000 --> 00:31:31,000
Comprehension.

573
00:31:32,000 --> 00:31:33,000
Okay.

574
00:31:33,000 --> 00:31:34,000
Comprehension.

575
00:31:35,000 --> 00:31:41,000
Now the best way over here is that I will go ahead and create even variable.

576
00:31:41,000 --> 00:31:48,000
Now first of all for x in range of ten let me just go ahead and take ten variables.

577
00:31:48,000 --> 00:31:53,000
Okay I know what I need to display x square okay.

578
00:31:53,000 --> 00:31:55,000
But I will write one more condition.

579
00:31:55,000 --> 00:32:04,000
If if x modulus two is double equal to zero I will go ahead and print my events okay.

580
00:32:04,000 --> 00:32:06,000
Now let me just go ahead and execute it.

581
00:32:06,000 --> 00:32:10,000
So this is zero column zero two colon four four.

582
00:32:10,000 --> 00:32:11,000
Column 16 six.

583
00:32:11,000 --> 00:32:12,000
Colon 36 eight.

584
00:32:12,000 --> 00:32:13,000
Colon 64.

585
00:32:13,000 --> 00:32:14,000
Right.

586
00:32:14,000 --> 00:32:18,000
So this is in short giving me the square of only even numbers.

587
00:32:18,000 --> 00:32:22,000
Right now these are some of the examples.

588
00:32:22,000 --> 00:32:23,000
Again dictionary comprehension.

589
00:32:23,000 --> 00:32:26,000
You can like how do you execute with respect to list.

590
00:32:26,000 --> 00:32:30,000
Similarly you can also do it with respect to uh ordinaries only.

591
00:32:30,000 --> 00:32:35,000
The thing is that you need to have the output over here in the form of key value pairs.

592
00:32:35,000 --> 00:32:36,000
Okay.

593
00:32:36,000 --> 00:32:38,000
So this was one of the most amazing thing.

594
00:32:38,000 --> 00:32:39,000
Okay.

595
00:32:39,000 --> 00:32:46,000
Let me just show you some of the practical examples that may be beneficial for understanding, um,

596
00:32:46,000 --> 00:32:48,000
some more practical examples.

597
00:32:48,000 --> 00:32:54,000
And in the next video I will also talk about some of the amazing use cases where all this list, dictionaries

598
00:32:54,000 --> 00:32:55,000
or dictionaries will be used.

599
00:32:55,000 --> 00:32:56,000
Okay.

600
00:32:56,000 --> 00:32:59,000
So practical examples okay.

601
00:32:59,000 --> 00:33:04,000
Now with respect to this practical example I will give you a task.

602
00:33:04,000 --> 00:33:06,000
Use a dictionary.

603
00:33:08,000 --> 00:33:08,000
Okay.

604
00:33:09,000 --> 00:33:18,000
Use a dictionary to count the frequency of elements in list okay.

605
00:33:19,000 --> 00:33:21,000
Now we'll go ahead and see this.

606
00:33:21,000 --> 00:33:23,000
Let's say I have numbers.

607
00:33:23,000 --> 00:33:28,000
Numbers is nothing, but it is in the list format one comma two comma two.

608
00:33:29,000 --> 00:33:36,000
comma, three comma, three comma, three comma, four comma four comma four comma four.

609
00:33:36,000 --> 00:33:36,000
Okay.

610
00:33:36,000 --> 00:33:37,000
So these are my numbers.

611
00:33:37,000 --> 00:33:42,000
Now you need to write a code using dictionary to count the frequency of every element in the list.

612
00:33:42,000 --> 00:33:46,000
Like one is present one time, two is present two times something like this.

613
00:33:46,000 --> 00:33:48,000
So I will go ahead and create.

614
00:33:48,000 --> 00:33:49,000
First of all you try it out.

615
00:33:49,000 --> 00:33:50,000
Okay.

616
00:33:50,000 --> 00:33:56,000
Just pause for five, five, at least five minutes and just try to write whatever is possible by you

617
00:33:56,000 --> 00:33:58,000
and then go ahead and see the solution.

618
00:33:58,000 --> 00:33:58,000
Okay.

619
00:33:58,000 --> 00:34:02,000
Now let me just go ahead and create a frequency empty variable.

620
00:34:02,000 --> 00:34:08,000
Then I will go ahead and write for number in numbers.

621
00:34:08,000 --> 00:34:09,000
Okay.

622
00:34:09,000 --> 00:34:12,000
First of all I will iterate through each and every number.

623
00:34:12,000 --> 00:34:16,000
Then I will go ahead and write if number in frequency.

624
00:34:17,000 --> 00:34:17,000
Okay.

625
00:34:17,000 --> 00:34:20,000
If the number is basically present in the frequency, then it is fine.

626
00:34:21,000 --> 00:34:28,000
Then what we do, we basically take that frequency of number and we keep on incrementing it.

627
00:34:28,000 --> 00:34:28,000
Right.

628
00:34:28,000 --> 00:34:29,000
Plus is equal to one.

629
00:34:30,000 --> 00:34:35,000
Now what exactly this is basically doing this is with respect to the key and with respect to that particular

630
00:34:35,000 --> 00:34:39,000
key I need to just increment keep on incrementing the value saying that hey it is present one time two

631
00:34:39,000 --> 00:34:43,000
time three time Like when two times basically comes, right?

632
00:34:43,000 --> 00:34:46,000
So first time it will be zero and then it will go to one, two, three like that okay.

633
00:34:47,000 --> 00:34:55,000
Else if it is coming for the first time then we should basically assign that value to one, right.

634
00:34:56,000 --> 00:34:57,000
Otherwise it will keep on incrementing.

635
00:34:57,000 --> 00:34:59,000
So number is equal to one.

636
00:34:59,000 --> 00:35:02,000
Now let me just go ahead and print the frequency.

637
00:35:02,000 --> 00:35:07,000
So here you'll be able to understand how many number of times one is present, two is present and three

638
00:35:07,000 --> 00:35:07,000
is present.

639
00:35:07,000 --> 00:35:08,000
Just see this code.

640
00:35:08,000 --> 00:35:10,000
Observe this code okay.

641
00:35:10,000 --> 00:35:13,000
If I go ahead and execute it now here you can see numbers.

642
00:35:13,000 --> 00:35:14,000
It is iterating.

643
00:35:14,000 --> 00:35:16,000
If number is in the frequency initially it is not present.

644
00:35:16,000 --> 00:35:18,000
So what it will do it will go over here.

645
00:35:18,000 --> 00:35:21,000
Take this number and it will assign the value one.

646
00:35:21,000 --> 00:35:23,000
Then it will go again back to the next number.

647
00:35:23,000 --> 00:35:26,000
Two is present number is not present in the frequency.

648
00:35:26,000 --> 00:35:28,000
It will again go back to the else loop.

649
00:35:28,000 --> 00:35:29,000
And again it will update one.

650
00:35:29,000 --> 00:35:32,000
Then again it will go back okay.

651
00:35:32,000 --> 00:35:33,000
Then the next number is two.

652
00:35:33,000 --> 00:35:35,000
This time it is present in the frequency.

653
00:35:35,000 --> 00:35:36,000
So it will go inside.

654
00:35:36,000 --> 00:35:41,000
This frequency of two is equal to plus is equal to one right plus equal to one.

655
00:35:41,000 --> 00:35:43,000
So that basically we are just going to increment one with one.

656
00:35:43,000 --> 00:35:44,000
So it will become two.

657
00:35:44,000 --> 00:35:46,000
So how many number of times two is present.

658
00:35:46,000 --> 00:35:48,000
Only that much will be remaining three.

659
00:35:48,000 --> 00:35:52,000
It will increment till three four it will increment till four because there are four number of fours.

660
00:35:52,000 --> 00:35:53,000
Okay.

661
00:35:53,000 --> 00:35:53,000
okay.

662
00:35:53,000 --> 00:35:56,000
So this is how you specifically get one amazing thing okay.

663
00:35:57,000 --> 00:36:00,000
Now let me just show you another one example.

664
00:36:00,000 --> 00:36:02,000
And this is probably we have not discussed.

665
00:36:02,000 --> 00:36:06,000
Also let's go ahead and merge two dictionaries into one okay.

666
00:36:06,000 --> 00:36:10,000
There may be scenario that I may have two dictionaries and I really want to merge them into one.

667
00:36:10,000 --> 00:36:11,000
Okay.

668
00:36:11,000 --> 00:36:15,000
This is one kind of hack that I really want to show it to you.

669
00:36:15,000 --> 00:36:20,000
So dictionary one and this is what we will understand when we understand about functions, because there

670
00:36:20,000 --> 00:36:25,000
is one very key important parameter which is called as positional argument and keyword argument.

671
00:36:25,000 --> 00:36:25,000
Okay.

672
00:36:26,000 --> 00:36:29,000
Uh, why we use Asterix mask and all.

673
00:36:29,000 --> 00:36:31,000
I'll just discuss when we discuss about the function.

674
00:36:31,000 --> 00:36:37,000
So now let me say that hey, there is my dictionary elements over here a is equal to one and this B

675
00:36:37,000 --> 00:36:40,000
is nothing but equal to two okay.

676
00:36:40,000 --> 00:36:42,000
Similarly I may have a different dictionary.

677
00:36:42,000 --> 00:36:53,000
The second dictionary here B colon three and c is colon four.

678
00:36:54,000 --> 00:36:54,000
Okay.

679
00:36:54,000 --> 00:37:01,000
Now if I go ahead and write merge dictionary okay.

680
00:37:02,000 --> 00:37:07,000
Now see if I go ahead and try to merge this dictionary.

681
00:37:07,000 --> 00:37:09,000
I will use two amazing things over here.

682
00:37:09,000 --> 00:37:11,000
One is asterisk.

683
00:37:11,000 --> 00:37:12,000
Asterisk.

684
00:37:12,000 --> 00:37:13,000
Dictionary one okay.

685
00:37:13,000 --> 00:37:17,000
Comma asterisk asterisk dictionary two.

686
00:37:17,000 --> 00:37:24,000
Now, the best thing about this asterisk is that any number of a number of key value pairs.

687
00:37:24,000 --> 00:37:24,000
Right.

688
00:37:24,000 --> 00:37:29,000
If I'm giving two double asterisk any number of key value pairs, that is probably going to consider

689
00:37:29,000 --> 00:37:30,000
come.

690
00:37:30,000 --> 00:37:34,000
It is just going to take that as a key value pair, and it is just going to append it inside this particular

691
00:37:34,000 --> 00:37:35,000
dictionary okay.

692
00:37:36,000 --> 00:37:38,000
This is basically called as a keyword argument okay.

693
00:37:38,000 --> 00:37:43,000
And over here you can see in dictionary one and dictionary two both are keywords argument okay.

694
00:37:43,000 --> 00:37:47,000
So keywords argument basically means any value that is present in the form of key value pairs.

695
00:37:47,000 --> 00:37:48,000
Okay.

696
00:37:48,000 --> 00:37:53,000
Now if I go ahead and print this merge dictionary and we'll discuss more about that in functions okay.

697
00:37:54,000 --> 00:37:57,000
So here you can see that it has merged very much easily okay.

698
00:37:58,000 --> 00:38:01,000
So I hope you got an idea about dictionaries.

699
00:38:02,000 --> 00:38:06,000
We have discussed so many different lines of code I have written over here in front of you, and almost

700
00:38:06,000 --> 00:38:08,000
each and every topics with some examples.

701
00:38:08,000 --> 00:38:13,000
And all I have actually explained, explained in a way that you should be able to understand a very

702
00:38:13,000 --> 00:38:14,000
lame language.

703
00:38:14,000 --> 00:38:16,000
So I hope you like this particular video.

704
00:38:16,000 --> 00:38:17,000
This was it for my side.

705
00:38:17,000 --> 00:38:18,000
I will see you all in the next video.

706
00:38:18,000 --> 00:38:18,000
Thank you.

707
00:38:18,000 --> 00:38:18,000
Take care.

