1
00:00:06,070 --> 00:00:07,700
Welcome back everyone in this lecture.

2
00:00:07,710 --> 00:00:14,350
We're going to talk about dictionaries on dictionaries are ordered mappings for storing objects.

3
00:00:14,350 --> 00:00:20,440
Previously we saw how store objects in an ordered sequence dictionaries instead are going to use a key

4
00:00:20,440 --> 00:00:21,900
value pair system.

5
00:00:22,240 --> 00:00:28,060
Basically this key value pair allows users to quickly grab objects without needing to own index location

6
00:00:28,060 --> 00:00:28,910
beforehand.

7
00:00:30,500 --> 00:00:35,930
The actual syntax is that dictionaries use curly braces and colons to signify the keys and their associated

8
00:00:35,930 --> 00:00:36,750
values.

9
00:00:36,770 --> 00:00:42,200
So here we have an example of a python dictionary with one key value pair key one in value one and then

10
00:00:42,200 --> 00:00:44,760
another key value pair key to add value to.

11
00:00:44,930 --> 00:00:48,830
So we have curly braces on the outside and then each individual key value pair.

12
00:00:48,860 --> 00:00:53,690
We have a string key colon and then its corresponding value and its value doesn't need to be a string.

13
00:00:53,690 --> 00:00:57,720
It could be a number a list something else.

14
00:00:57,740 --> 00:00:58,440
Boolean.

15
00:00:58,520 --> 00:01:01,610
Lots of other options there and it can even be another dictionary.

16
00:01:01,970 --> 00:01:07,970
But the main idea here is if I'm looking for value one then all I need to do is pass in Q1 and will

17
00:01:07,970 --> 00:01:09,610
bring me back that value 1.

18
00:01:09,830 --> 00:01:14,990
Or if I'm looking for a value to I can pasand key 2 and I'll bring it back value to and we're actually

19
00:01:14,990 --> 00:01:19,700
coding this out and the Adam text editor will see some examples that kind of clarify the use cases for

20
00:01:19,700 --> 00:01:24,770
a dictionary but often beginners have the question while why how is this different than a list.

21
00:01:24,770 --> 00:01:27,560
Why should I choose a list and when should I choose a dictionary.

22
00:01:27,560 --> 00:01:29,810
That's often a point of confusion.

23
00:01:29,810 --> 00:01:35,410
The basic idea here is that dictionaries those objects are values are retrieved by the key name.

24
00:01:35,600 --> 00:01:41,530
So if I'm interested in value one all I need to know is that its associated key is key one unlike a

25
00:01:41,540 --> 00:01:45,980
list where I would have had to have known the index location beforehand.

26
00:01:45,980 --> 00:01:51,230
Now the tradeoff here is that because the objects are retrieved by a key name that means there's no

27
00:01:51,230 --> 00:01:54,020
real order to them so you can't really sort a dictionary.

28
00:01:54,050 --> 00:01:56,600
It's instead the key value pair mappings.

29
00:01:56,600 --> 00:01:59,590
Now list those objects are retrieved by location.

30
00:01:59,660 --> 00:02:04,800
And what's nice about that is that they can be indexed or sliced because it's an ordered sequence.

31
00:02:04,880 --> 00:02:09,570
The con there is that if you actually want to grab something an individual element from the list you

32
00:02:09,590 --> 00:02:12,740
would need to know its actual index location beforehand.

33
00:02:12,740 --> 00:02:13,940
So essentially that's a tradeoff.

34
00:02:13,940 --> 00:02:18,140
We're going to see some more examples of this idea of dictionaries and the key value pair mapping right

35
00:02:18,140 --> 00:02:21,900
now by hopping over to the text editor and working through some examples.

36
00:02:21,920 --> 00:02:23,320
So let's head over to that now.

37
00:02:23,330 --> 00:02:26,380
All right what I'm going to start off with is just creating a very simple dictionary.

38
00:02:26,480 --> 00:02:36,640
So we'll say D is the variable and we're going to set that equal to key one and value 1 and the other

39
00:02:36,640 --> 00:02:41,320
key is going to be key to and then value 2.

40
00:02:41,320 --> 00:02:44,960
So then let's print out the dictionary and see what that looks like.

41
00:02:47,550 --> 00:02:50,970
There we go so we have one that value 1 and key to value 2.

42
00:02:50,970 --> 00:02:55,530
Now let's imagine I wanted to actually grab a corresponding key value pair.

43
00:02:55,530 --> 00:02:57,730
What I can do is the following.

44
00:02:57,880 --> 00:02:59,860
I can say the square brackets.

45
00:02:59,860 --> 00:03:06,330
But now instead of passing an index location like it would for a list or a string I pass in the Keitt.

46
00:03:06,520 --> 00:03:11,480
So I'll say key one and then let's print the that response.

47
00:03:11,490 --> 00:03:15,420
Will say print out d of key one save that change.

48
00:03:15,420 --> 00:03:19,810
Run this again and now I see value 1 is the output as well as that dictionary from before.

49
00:03:20,070 --> 00:03:22,350
So that's the way you can grab values.

50
00:03:22,350 --> 00:03:25,640
Now let's go ahead and show you a more realistic example.

51
00:03:25,680 --> 00:03:31,010
Let's say I have a list called salaries or excuse me a dictionary called salaries.

52
00:03:31,170 --> 00:03:35,960
So this is a really great idea for storing information if a dictionary instead of a list.

53
00:03:36,130 --> 00:03:41,830
So I'm going to create a salary's dictionary and the key is going to be someone's name and then the

54
00:03:41,830 --> 00:03:47,590
actual value is going to be how much their salary is let's say we'll do it by our.

55
00:03:47,590 --> 00:03:49,630
So John earns $20 an hour.

56
00:03:49,690 --> 00:03:57,310
Notice here I'm just passing in a number and then we'll say Sally earns $30 an hour and let's say Sammy

57
00:03:58,390 --> 00:04:00,530
he's going to earn $15 an hour.

58
00:04:00,970 --> 00:04:05,940
And now the question arises Well how much does John earn per hour how much does Sammy earn per hour.

59
00:04:05,950 --> 00:04:11,050
Now it's really readable because all I need to do is say salaries and then just pass in the name of

60
00:04:11,080 --> 00:04:12,940
whose salary I'm interested in.

61
00:04:12,940 --> 00:04:21,650
So then I can say Sallie and then if I just print this out so print out salaries for Sallie and let

62
00:04:21,650 --> 00:04:26,990
me delete these lines right here I run this again and then I get back 30.

63
00:04:26,990 --> 00:04:30,830
So this is a really good use case for what you would want to use a dictionary for.

64
00:04:30,830 --> 00:04:35,930
You have a key value pairing and what you're going to be interested in is looking up these keys and

65
00:04:35,930 --> 00:04:38,900
then getting back their corresponding values something like this.

66
00:04:38,900 --> 00:04:43,820
When it really makes sense for a list because then that would require you to memorize every single pair

67
00:04:43,820 --> 00:04:48,550
location and it would really make sense because then you'd be mixing in names and numbers.

68
00:04:48,560 --> 00:04:53,890
So instead of using a dictionary like this allows you to easily look things up based off the key.

69
00:04:53,970 --> 00:04:55,460
In this case the key is quite intuitive.

70
00:04:55,460 --> 00:04:59,900
It's just that person's first name and then the value here is their salaries.

71
00:04:59,900 --> 00:05:04,270
Now let's imagine we just hired a new employee and we want to add them to the salaries dictionary that

72
00:05:04,300 --> 00:05:10,990
we do that is you call salaries and then you reference the key as if it already existed there.

73
00:05:11,030 --> 00:05:16,920
So let's say we just hired Cindy and then you set it equal to whatever the corresponding value was so

74
00:05:16,920 --> 00:05:18,900
let's say he's the CEO.

75
00:05:18,910 --> 00:05:23,190
So she's going to get paid $100 an hour and then just to check that it's working.

76
00:05:23,190 --> 00:05:30,970
Instead of asking for salaries or Sally's salary I'm going to ask for Cynthy's save that run.

77
00:05:31,260 --> 00:05:32,870
And then I get back 100.

78
00:05:33,120 --> 00:05:38,940
So here what I'm doing and just corresponding the Sindhi key with the value of 100.

79
00:05:38,940 --> 00:05:45,480
So that's how you can add values and key value pairs to an existing dictionary if you ever want to adjust

80
00:05:45,570 --> 00:05:46,380
a value pair.

81
00:05:46,380 --> 00:05:53,670
Let's say John got a raise then you need to do is say salaries call the existing He says John and let's

82
00:05:53,670 --> 00:05:55,170
give them a raise.

83
00:05:55,170 --> 00:05:56,830
So there's two ways of doing this.

84
00:05:56,850 --> 00:06:03,120
One is I could just set equal to a new value saying let's say John's new salary is 30 or I can actually

85
00:06:03,120 --> 00:06:05,540
reference the old value I could say.

86
00:06:05,580 --> 00:06:05,880
OK.

87
00:06:05,880 --> 00:06:14,120
Grab John's old salary value add that add let's say 42 it so got a big raise there.

88
00:06:14,120 --> 00:06:16,060
Let's check if that worked.

89
00:06:16,060 --> 00:06:25,100
So I'm going to create that dictionary of salaries and then I'm going to print John's salary right after

90
00:06:25,100 --> 00:06:26,770
we make it so we should see 20.

91
00:06:27,080 --> 00:06:31,430
And then we're going to leave this in the line here that I'm going to give him a raise.

92
00:06:31,430 --> 00:06:34,970
And then it prints his salary again.

93
00:06:35,030 --> 00:06:38,370
So we should see 20 and then 60 when I run this.

94
00:06:38,780 --> 00:06:40,560
And there it is 20 and then 60.

95
00:06:40,580 --> 00:06:42,070
So it starts off with $20.

96
00:06:42,140 --> 00:06:48,050
But then I adjust it to his existing salary plus 40 something else I could just set it straight to 40.

97
00:06:48,200 --> 00:06:52,190
And then if I run if I were to run this again that I would see 20 and 40.

98
00:06:52,190 --> 00:06:56,950
OK so that's how you can add new key value pairs as well as just old key value pairs.

99
00:06:57,140 --> 00:07:02,510
Something else you can do which is not super common but it is available to you is passen things like

100
00:07:02,510 --> 00:07:05,260
dictionaries or lists as values.

101
00:07:05,300 --> 00:07:10,330
So for example let's say we want this to be defined as people instead.

102
00:07:10,330 --> 00:07:16,250
So we have people let's say we have John and Sally let's just delete samea off of this and they can

103
00:07:16,250 --> 00:07:22,380
have multiple aspects linked to them by passing and a list as that value pair.

104
00:07:22,400 --> 00:07:28,520
So say John and we can give him some numbers like 1 2 3 and then we can say OK salary or excuse me Sally

105
00:07:28,580 --> 00:07:32,570
is going to have some other metrics like 40 10 and 30.

106
00:07:32,570 --> 00:07:35,410
Right now these numbers are just made out they don't really represent anything.

107
00:07:35,450 --> 00:07:42,020
I just want to show you they can actually link a list to this value or this keep pair so that I could

108
00:07:42,020 --> 00:07:47,250
say OK people John and let's print that out.

109
00:07:50,010 --> 00:07:55,210
Print people John print that and now and I run this I get back the list.

110
00:07:55,210 --> 00:07:58,650
One two three which means I can then do something like this.

111
00:07:58,810 --> 00:08:05,470
I could say Sally and then let's grab that index with zero from this list so I can actually make a stack

112
00:08:05,500 --> 00:08:06,880
call here.

113
00:08:06,940 --> 00:08:10,710
So keep in mind just Pozieres video and think what you should see as the output.

114
00:08:10,780 --> 00:08:13,370
If I ask for people Sally 0.

115
00:08:13,790 --> 00:08:18,670
OK hopefully understood it's going to be 40 because when I run this what's happening is we first grab

116
00:08:18,940 --> 00:08:24,180
this list which is people Salli and then we're going to grab in the first item of this list.

117
00:08:24,190 --> 00:08:24,660
Zero.

118
00:08:24,700 --> 00:08:26,540
Again these numbers don't really represent anything.

119
00:08:26,650 --> 00:08:28,840
They're just here for demonstration purposes.

120
00:08:29,830 --> 00:08:32,500
And if you really want you can also have a nested dictionary.

121
00:08:32,680 --> 00:08:34,520
So this can be a further dictionary.

122
00:08:34,570 --> 00:08:43,330
So you could say something like salary 10 and then something like age 30 and let's go ahead and delete

123
00:08:43,360 --> 00:08:44,720
this.

124
00:08:44,840 --> 00:08:47,740
So this is now a nested dictionary for John.

125
00:08:48,030 --> 00:08:55,690
So then I could say people John and then go again and ask for age and then print that out.

126
00:08:57,160 --> 00:09:02,490
And this is starting to look almost like Jaison if you're familiar with that so I can print this and

127
00:09:02,490 --> 00:09:03,510
I get back 30.

128
00:09:03,540 --> 00:09:05,700
So this is an example of a nested dictionary.

129
00:09:05,700 --> 00:09:08,460
Again calling John and then getting back that dictionary.

130
00:09:08,620 --> 00:09:10,570
So you can always break this down by steps.

131
00:09:10,590 --> 00:09:12,510
Let's cut out age there.

132
00:09:12,570 --> 00:09:17,040
So if I call people John I get back to this dictionary and then off of that dictionary I can then call

133
00:09:17,040 --> 00:09:23,520
off more keys so I could say something like salary save that run this again and then I get back $10

134
00:09:23,520 --> 00:09:25,260
per hour as his salary.

135
00:09:25,260 --> 00:09:30,720
Last thing I want to mention is that there's a few useful pairs or useful method calls you can call

136
00:09:30,720 --> 00:09:31,380
for.

137
00:09:31,380 --> 00:09:34,160
So let me show you that and create a very simple dictionary right now.

138
00:09:34,440 --> 00:09:37,870
We'll say K-1 V-1.

139
00:09:37,920 --> 00:09:46,290
So say K-1 10 K-2 has a value of 20 and we'll say K3 has a value of 30.

140
00:09:46,550 --> 00:09:52,020
And if I print out the dictionary I'll see everything.

141
00:09:52,060 --> 00:09:53,440
So I see this whole dictionary.

142
00:09:53,590 --> 00:09:55,740
But I can also have methods for this.

143
00:09:55,780 --> 00:10:01,120
So if I just want the keys of a dictionary I can call keys open close print sees and or turnback the

144
00:10:01,120 --> 00:10:06,340
dictionary keys if I want just the values of a dictionary.

145
00:10:06,400 --> 00:10:12,640
I can call it the values run that and then I get back the values and if I want the actual key value

146
00:10:12,640 --> 00:10:19,180
pairs I can call items and I'll return back this list object of these tuple key value pairs and we're

147
00:10:19,180 --> 00:10:21,580
going to talk about tuples in the very next lecture.

148
00:10:21,580 --> 00:10:23,090
So keep that in mind.

149
00:10:23,150 --> 00:10:28,570
OK so those are keys values and items which are kind of convenient when you want to iterate through

150
00:10:28,570 --> 00:10:32,900
a dictionary and we'll talk about that when we talk about control flow and for loops.

151
00:10:32,980 --> 00:10:38,180
But right now an idea should be understanding the syntax how to add new items or key value pairs to

152
00:10:38,180 --> 00:10:40,600
a dictionary and then those key methods.

153
00:10:40,600 --> 00:10:43,610
All right if you have any questions feel free to post the Q&amp;A forums.

154
00:10:43,630 --> 00:10:44,870
We'll see at the next lecture.
