1
00:00:05,420 --> 00:00:10,580
Welcome back, everyone, to our continued discussion of Python basics in this lecture, we're talking

2
00:00:10,580 --> 00:00:11,510
about dictionaries.

3
00:00:12,540 --> 00:00:16,740
Dictionaries allow us to store data in what is known as a key value pair.

4
00:00:17,070 --> 00:00:21,120
This idea of key value pair is actually going to show up a lot, especially when we begin to talk about

5
00:00:21,120 --> 00:00:21,540
Django.

6
00:00:21,550 --> 00:00:27,450
So keeping in mind a key value pair is essentially matching a unique key to some sort of value.

7
00:00:27,780 --> 00:00:32,430
That way, when you want to actually look up the value, you just need to know its reference key and

8
00:00:32,430 --> 00:00:35,820
notice how keys aren't typically going to be integers or numbers.

9
00:00:36,210 --> 00:00:41,580
We tend to use dictionaries when it makes more sense to organize data lookup by a key rather than by

10
00:00:41,580 --> 00:00:42,690
a particular position.

11
00:00:43,080 --> 00:00:49,620
So even though the latest generations of Python will actually remember the order that items and key

12
00:00:49,620 --> 00:00:54,840
value pairs are inserted into a dictionary, you shouldn't really be thinking about ordering or sequencing

13
00:00:54,840 --> 00:00:58,710
too much when thinking about a dictionary and looking up things from a dictionary.

14
00:00:59,040 --> 00:01:03,030
If sequence and ordering is important to you, think back to a list.

15
00:01:03,450 --> 00:01:06,930
Key value pairs is kind of a different way to think about storing items.

16
00:01:07,830 --> 00:01:12,060
So we're going to do is explore dictionaries with Python as well as their use cases.

17
00:01:12,300 --> 00:01:13,530
Let's head to our code editor.

18
00:01:14,070 --> 00:01:15,990
OK, here I am inside our editor.

19
00:01:16,170 --> 00:01:18,300
Let's go ahead and show you how to make a dictionary.

20
00:01:18,390 --> 00:01:22,200
So the way a dictionary works is you go ahead and set up your variable name.

21
00:01:22,530 --> 00:01:25,470
Let's go ahead and call this dictionary employees.

22
00:01:26,690 --> 00:01:32,270
Let's imagine we're trying to keep track of employees and what we're going to do here is just say something

23
00:01:32,270 --> 00:01:34,250
like, let's say our chef.

24
00:01:35,950 --> 00:01:36,790
Is Amy.

25
00:01:38,980 --> 00:01:45,580
And then let's say we have another position like our CEO, and let's say that one is Jason.

26
00:01:46,070 --> 00:01:50,890
OK, so I have employees here and this is now a dictionary again.

27
00:01:50,890 --> 00:01:53,410
It's curly braces for the dictionary.

28
00:01:53,620 --> 00:01:57,340
And then each key value item pair is separated by a comma.

29
00:01:57,700 --> 00:02:03,250
This first string here is going to be the key, which then connects to the value Amy.

30
00:02:03,610 --> 00:02:08,620
And keep in mind, it doesn't need to be technically strings on either of these keys are pretty much

31
00:02:08,620 --> 00:02:10,840
always going to be strings, and you'll see why in a second.

32
00:02:10,900 --> 00:02:13,450
It just makes a lot more sense to look things up like that.

33
00:02:13,660 --> 00:02:21,040
But the actual values themselves could also just be the numbers are integers or even other dictionaries,

34
00:02:21,040 --> 00:02:21,710
other lists.

35
00:02:21,730 --> 00:02:27,700
For example, I could say for employees, we could say like number or something like that and put in

36
00:02:27,700 --> 00:02:29,530
45 or something like that.

37
00:02:29,540 --> 00:02:30,850
So just keep that in mind.

38
00:02:30,890 --> 00:02:32,200
Doesn't necessarily need to be strings.

39
00:02:32,350 --> 00:02:36,850
But what I want to show you is how you would actually look up information from a dictionary.

40
00:02:37,630 --> 00:02:44,770
Now, since I have employees as a dictionary, I want to be thinking and things of key value pairs.

41
00:02:45,100 --> 00:02:47,020
So what does employees actually represent?

42
00:02:47,380 --> 00:02:53,320
Employees represents a key value pairing of all the positions across the company.

43
00:02:53,530 --> 00:02:55,240
And then who is in that position?

44
00:02:55,690 --> 00:03:01,000
So what I can think of is, OK, I'm interested in who is the CEO of my company.

45
00:03:01,360 --> 00:03:06,610
So now when I look it up instead of previously or looking things up, I index position by passing the

46
00:03:06,610 --> 00:03:12,850
zero or one, etc. I'm looking things up by their key, so I would look up CEO and let's go ahead and

47
00:03:12,850 --> 00:03:15,730
print that result so I can actually see it.

48
00:03:16,010 --> 00:03:18,550
So we're going to say print employees see you.

49
00:03:18,850 --> 00:03:22,540
You'll notice it still uses the same square bracket notation as indexing did.

50
00:03:22,840 --> 00:03:25,930
But what you actually pass in is going to be the key.

51
00:03:26,450 --> 00:03:27,430
So I'm going to save that.

52
00:03:27,430 --> 00:03:33,010
Let's run example that PI and now I can see Jason's being reported back.

53
00:03:33,310 --> 00:03:34,630
So notice how useful this is.

54
00:03:34,990 --> 00:03:40,550
I now have almost like this Rolodex object, or I just need to remember that indexing key.

55
00:03:40,780 --> 00:03:43,480
And then I can get back the relevant value.

56
00:03:43,870 --> 00:03:48,160
And what's also really nice is that I can be able to keep these keys.

57
00:03:48,340 --> 00:03:53,620
But then if somebody changes, for example, let's say we hire a new position at the company, I can

58
00:03:53,620 --> 00:03:55,060
go ahead and easily add that in.

59
00:03:55,570 --> 00:03:58,090
So again, it's key value pairs for the dictionaries.

60
00:03:58,300 --> 00:04:01,420
And now let me show you how to add in a new employee.

61
00:04:02,350 --> 00:04:08,630
So let's imagine we just created a new position, which means I need to add in a new key to my dictionary.

62
00:04:08,650 --> 00:04:09,310
How do I do that?

63
00:04:09,910 --> 00:04:11,950
Well, we call your dictionary.

64
00:04:12,040 --> 00:04:14,200
In this case, we called our dictionary employees.

65
00:04:14,560 --> 00:04:21,279
And then what we're going to do here is in square brackets, you refer to the new key you want to add

66
00:04:21,279 --> 00:04:24,100
in almost as if it already existed in the dictionary.

67
00:04:24,640 --> 00:04:30,370
So let's imagine we just hired a waiter at our company because maybe we have a restaurant style company.

68
00:04:30,820 --> 00:04:32,530
So I say, OK, we just hired a waiter.

69
00:04:33,750 --> 00:04:35,940
And now I'm going to set that waiter equal to.

70
00:04:36,210 --> 00:04:38,850
And let's say I hired Mike to be the waiter.

71
00:04:40,080 --> 00:04:44,910
So now I can go ahead and print employees, I'm going to print out that whole dictionary.

72
00:04:45,570 --> 00:04:46,590
Let's go ahead and save that.

73
00:04:47,460 --> 00:04:53,940
And when I run Python example that I notice that I was able to add in a waiter and mike.

74
00:04:54,420 --> 00:05:00,660
So if I ever want to know who the waiter is, I could just look up employees and then pass in the position

75
00:05:00,660 --> 00:05:01,860
I was interested in, like waiter.

76
00:05:04,530 --> 00:05:05,550
And I can see, OK.

77
00:05:05,580 --> 00:05:12,510
It was, Mike, that way in the future, if something gets changed or updated, I can go ahead and easily

78
00:05:12,540 --> 00:05:14,850
figure out, OK, who is it now in that position?

79
00:05:15,570 --> 00:05:20,220
So I have employees and then I know how to add in a new key with a new value.

80
00:05:20,250 --> 00:05:24,960
So this is adding in a new key value pair.

81
00:05:26,730 --> 00:05:29,130
Now, what if I want to update a new key value pair?

82
00:05:29,160 --> 00:05:31,560
Maybe we got a new chef, how do we actually take care of that?

83
00:05:31,800 --> 00:05:33,420
Well, what I can do.

84
00:05:34,520 --> 00:05:43,070
Is the following if I want to perform an update to an existing key value pair, what I have to do is

85
00:05:43,070 --> 00:05:49,340
I call again employees and then I grab the key I want to update, for example, maybe I want to update

86
00:05:49,340 --> 00:05:49,780
the chef.

87
00:05:50,420 --> 00:05:52,040
And let's say I'm not a chef's.

88
00:05:52,040 --> 00:05:54,350
I'm going to update this to be Jose.

89
00:05:55,310 --> 00:06:00,170
Which means I'm going to print out employees and let's go ahead and print out the chef.

90
00:06:00,710 --> 00:06:05,930
And I'm actually going to print out chef and both so we can see that, in fact, it was updated.

91
00:06:06,260 --> 00:06:12,560
So I'm first going to print out employee chef with the original dictionary here, and then I'll go ahead

92
00:06:12,560 --> 00:06:13,550
and print out.

93
00:06:15,240 --> 00:06:15,810
Update.

94
00:06:16,890 --> 00:06:19,500
Chef and then I will update chef here.

95
00:06:19,800 --> 00:06:23,550
Kind of similar to the syntax of adding in a new key value pair.

96
00:06:23,820 --> 00:06:28,560
And then we're going to print this out, so the output I should see will be Amy Update Chef Jose.

97
00:06:29,640 --> 00:06:33,480
So let's go ahead and explore that and go ahead and kind of pauses VIDEO If you need to take a look

98
00:06:33,480 --> 00:06:34,290
at the syntax there.

99
00:06:34,920 --> 00:06:39,810
But let's run this and I see Amy update chef and then Jose.

100
00:06:40,590 --> 00:06:40,940
All right.

101
00:06:41,280 --> 00:06:48,630
So now we understand that I have the ability to add in new key value pairs, as well as update existing

102
00:06:48,630 --> 00:06:49,620
key value pairs.

103
00:06:50,100 --> 00:06:56,040
The other thing I want to point out here is remember, I'm actually getting back an object here of what

104
00:06:56,040 --> 00:06:56,940
that value was.

105
00:06:57,000 --> 00:07:03,240
In this case, it was the string Jose, which means I can actually perform actions on that object so

106
00:07:03,240 --> 00:07:04,740
I could print employees.

107
00:07:04,740 --> 00:07:08,510
Chef, but employee chef, if I just have, this code is going to be Amy.

108
00:07:08,530 --> 00:07:09,630
So let's see that right now.

109
00:07:10,260 --> 00:07:16,110
This will be Amy, which is a string object, which means I can actually call things like up here on

110
00:07:16,110 --> 00:07:18,080
this open and close parentheses.

111
00:07:18,990 --> 00:07:20,250
And it will work just fine.

112
00:07:20,940 --> 00:07:27,540
So the upper is being called on the actual string that's returned with employees chef, which means

113
00:07:27,540 --> 00:07:29,730
I now get back Amy in all caps.

114
00:07:30,120 --> 00:07:36,060
So I just want you to keep that in mind that as you begin to store different types of objects in a dictionary,

115
00:07:36,270 --> 00:07:41,130
you're going be able to use those objects or even assign those objects to a variable and then call methods

116
00:07:41,130 --> 00:07:41,550
on them.

117
00:07:41,970 --> 00:07:44,790
So let's actually show you another example.

118
00:07:45,390 --> 00:07:51,930
Let's say we are keeping track of different stock price histories, so I can say stock prices.

119
00:07:53,390 --> 00:07:55,040
And I'm going to create a dictionary here.

120
00:07:55,220 --> 00:07:58,880
Let's go ahead and create, let's say, the stock price for Google.

121
00:08:00,500 --> 00:08:05,690
And now this value is actually going to be a history of prices, which I'm going to represent as they

122
00:08:05,690 --> 00:08:10,910
list history of prices makes sense to keep as the list because I want to make sure that actually retains

123
00:08:10,910 --> 00:08:11,310
order.

124
00:08:11,330 --> 00:08:16,220
I don't want to accidentally have some sort of changing of the order because it's a history.

125
00:08:16,220 --> 00:08:20,960
It's things that probably shouldn't change order price on day one, price on day to price on day three

126
00:08:20,960 --> 00:08:21,560
and so on.

127
00:08:21,920 --> 00:08:23,270
So I'm going to make up some prices here.

128
00:08:23,570 --> 00:08:29,210
Let's say it's two hundred on the first day, 210 on the second day and then 220 on third day.

129
00:08:29,780 --> 00:08:32,419
Then I can put comma and add in another stock.

130
00:08:32,720 --> 00:08:36,650
So let's go ahead and say maybe Jimmy as a stock and it's going to be.

131
00:08:36,710 --> 00:08:37,760
This is just a stock ticker.

132
00:08:38,510 --> 00:08:45,560
Maybe it's 20 dollars on one day, then it just $100 on another day, maybe $300, etc. So this is a

133
00:08:45,560 --> 00:08:50,180
nice combination of a use for a dictionary and a use for a list.

134
00:08:50,540 --> 00:08:56,540
A dictionary allows me to quickly call something like stock prices, and this put in the ticker symbol

135
00:08:56,540 --> 00:08:58,220
that I'm interested in, like Google.

136
00:08:58,520 --> 00:09:02,540
And then this will actually be a list of the history of stock prices for that.

137
00:09:02,990 --> 00:09:05,120
So then I could just print this out.

138
00:09:05,450 --> 00:09:06,710
So let's go ahead and say print.

139
00:09:07,430 --> 00:09:14,210
Stock prices, Google run that example, and I could see 200 to 10 to 20.

140
00:09:14,540 --> 00:09:20,690
What I could also do is actually assigned this to something say like history is equal to stock prices

141
00:09:20,810 --> 00:09:21,530
of Google.

142
00:09:22,100 --> 00:09:26,940
And then maybe I only want to print out the first day of this actual stock price.

143
00:09:26,960 --> 00:09:27,620
How could I do that?

144
00:09:28,070 --> 00:09:34,460
I could use a bit of string literal formatting here and say first day price is.

145
00:09:35,880 --> 00:09:41,400
And then pass in history here, if I just pass on history by itself, they would actually print out

146
00:09:41,580 --> 00:09:44,070
this entire list if I only want the first day.

147
00:09:44,250 --> 00:09:47,490
I would just get history and index it at zero.

148
00:09:47,520 --> 00:09:51,570
Not that I'm actually putting in the indexing in the print format statement.

149
00:09:52,620 --> 00:09:58,860
So here I should see first day price is two hundred and know how I'm starting to combine different ideas.

150
00:09:59,100 --> 00:10:03,750
I'm starting to combine the idea of a list with a dictionary and then starting to combine that idea

151
00:10:03,750 --> 00:10:09,990
with a lookup and a variable assignment, then combining that with the idea of indexing and a string

152
00:10:09,990 --> 00:10:10,830
literal formatting.

153
00:10:11,070 --> 00:10:14,250
And hopefully you can begin to see that, you know, it's just these three lines of code.

154
00:10:14,610 --> 00:10:20,370
I have something with some logic, some sort of just common sense functionality of, OK, look up stock

155
00:10:20,370 --> 00:10:20,820
prices.

156
00:10:21,030 --> 00:10:27,060
I get the history and I can report that back to the user, which is honestly how most stock websites

157
00:10:27,060 --> 00:10:27,540
are going to work.

158
00:10:27,570 --> 00:10:29,620
They have some sort of way of storing the data.

159
00:10:29,910 --> 00:10:33,450
They have some sort of way of looking it up and then some sort of way of reporting it back to you.

160
00:10:34,910 --> 00:10:39,650
Now, the last thing I want to show you are just a couple of different ideas of nested dictionaries.

161
00:10:40,070 --> 00:10:43,190
Dictionaries are actually going to be pretty important when we first start learning Django.

162
00:10:43,250 --> 00:10:47,570
It's the format that Django uses to send information back to the web page.

163
00:10:47,870 --> 00:10:50,540
And the fact is you can actually nest dictionaries.

164
00:10:50,840 --> 00:10:54,350
This is not super common, but I do want to show you the fact that you can do it.

165
00:10:54,770 --> 00:11:00,290
So I'm going to say my dictionary here is equal to and I'm going to create a dictionary that contains

166
00:11:00,290 --> 00:11:01,070
another dictionary.

167
00:11:01,460 --> 00:11:04,880
So we're going to have a key here called outer colon.

168
00:11:05,270 --> 00:11:10,760
And then I'm going to put another set of curly braces here and then put a key called inner.

169
00:11:11,330 --> 00:11:14,730
And then let's just give this a value of 100.

170
00:11:15,410 --> 00:11:18,830
OK, so note that I have a dictionary within a dictionary.

171
00:11:18,860 --> 00:11:20,510
This is totally valid in Python.

172
00:11:20,930 --> 00:11:24,500
And the reason I'm pointing it out here is because there may be some times where we actually do this

173
00:11:24,500 --> 00:11:30,890
within Django and we're going to do here is I'm first going to print out my dictionary and then the

174
00:11:30,890 --> 00:11:33,240
key is going to be outor.

175
00:11:34,070 --> 00:11:38,210
So what do we actually expect to be returned here if I call outor?

176
00:11:38,600 --> 00:11:44,960
Well, just run this and I see I get back that dictionary, which means I could technically, if I wanted

177
00:11:45,020 --> 00:11:53,330
just one hundred, I could say my dictionary outer and then have another set of key look up and say

178
00:11:53,330 --> 00:11:55,010
returned back in her.

179
00:11:56,180 --> 00:11:57,050
Save that change.

180
00:11:57,950 --> 00:12:03,350
Go ahead and run this, and now I just get back the number one 100 so I can see some logic here of the

181
00:12:03,350 --> 00:12:07,580
outer key and then from this dictionary, grab the inner key value.

182
00:12:08,090 --> 00:12:11,900
OK, finally, a couple of methods that are important for dictionaries.

183
00:12:12,080 --> 00:12:13,730
Let's just quickly create a dictionary here.

184
00:12:15,140 --> 00:12:17,480
I want to create a dictionary with key one.

185
00:12:18,050 --> 00:12:24,050
This is just going to be a value of one hundred comma, let's say, key to a value of two.

186
00:12:24,830 --> 00:12:29,090
And then let's say key three and a value of four hundred.

187
00:12:30,610 --> 00:12:34,540
And now what I'm going to do is show you how to print just the keys.

188
00:12:34,690 --> 00:12:40,840
If you just want the keys, you can say my dictionary keys open close parentheses there.

189
00:12:41,080 --> 00:12:45,340
And if you run that, it'll just report back almost like a list of keys.

190
00:12:45,340 --> 00:12:47,470
It's technically a kind of a specialized object here.

191
00:12:47,590 --> 00:12:51,430
Dictionary keys, but can think of it as just a list, a key one key to key three.

192
00:12:51,790 --> 00:12:57,130
If you want just the values that has the numbers 100, 200 400, then you just call values.

193
00:12:57,490 --> 00:12:58,690
So let's check out that change.

194
00:12:59,680 --> 00:13:04,900
And here we just see the values 100, 200, 400 notice now, I don't actually know from either of these

195
00:13:04,930 --> 00:13:05,410
outputs.

196
00:13:05,650 --> 00:13:09,340
What are the key value pairs if you want those key value pairs?

197
00:13:09,670 --> 00:13:16,180
What you do is you call items and that's actually going to display these key value pairs for you.

198
00:13:17,280 --> 00:13:23,100
And you'll notice there's a use of parentheses here, and these are temples or temples.

199
00:13:23,340 --> 00:13:26,850
We haven't actually learned about tuples yet, so we're going to cover that in the next lecture.

200
00:13:27,030 --> 00:13:32,280
But I want you to just see it right now and discover, Oh, there is this other way of returning back

201
00:13:32,280 --> 00:13:36,540
this key value pairs in this tuple or tuple formation.

202
00:13:36,960 --> 00:13:40,530
OK, so that's it for the lecture on dictionaries.

203
00:13:40,800 --> 00:13:46,200
The main takeaway is to understand that there's key value pairs, and you can look them up using the

204
00:13:46,200 --> 00:13:49,300
indexing notation like this pass in.

205
00:13:49,300 --> 00:13:50,790
What's the actual string?

206
00:13:52,320 --> 00:13:52,920
He won.

207
00:13:53,520 --> 00:13:59,760
Save that and then run it, and then if you ever want to add a new item, you just call that dictionary.

208
00:14:00,270 --> 00:14:06,060
And if you want to update, you call an existing key like this and then do some sort of reassignment.

209
00:14:06,360 --> 00:14:09,750
Or if it's a totally brand new key, it's essentially the same syntax.

210
00:14:10,050 --> 00:14:13,890
You just specify the brand new key with some sort of default assignment.

211
00:14:14,430 --> 00:14:14,730
All right.

212
00:14:15,090 --> 00:14:16,080
That's it for this lecture.

213
00:14:16,200 --> 00:14:17,040
I'll see it the next one.

