1
00:00:00,380 --> 00:00:02,090
Hi guys, and welcome back.

2
00:00:02,090 --> 00:00:05,030
In this video we are going to talk about dictionaries.

3
00:00:05,030 --> 00:00:08,140
Dictionaries are another structure in Python,

4
00:00:08,140 --> 00:00:10,060
like lists, sets, and doubles,

5
00:00:10,060 --> 00:00:13,600
that allow us to interact with data in a certain way.

6
00:00:13,600 --> 00:00:15,310
And what dictionaries are for

7
00:00:15,310 --> 00:00:18,870
is to associate keys and values together

8
00:00:18,870 --> 00:00:20,510
so that if you know the key

9
00:00:20,510 --> 00:00:23,430
you can access the value very easily.

10
00:00:23,430 --> 00:00:25,190
For example, you could have a dictionary

11
00:00:25,190 --> 00:00:27,730
of your friend names being the keys

12
00:00:27,730 --> 00:00:29,880
and their ages being the values.

13
00:00:29,880 --> 00:00:31,730
That way if you knew the name of your friend,

14
00:00:31,730 --> 00:00:34,840
you can very easily get the value out, their age.

15
00:00:34,840 --> 00:00:35,740
Let's get started.

16
00:00:36,890 --> 00:00:39,530
Let's say we have a dictionary friend ages

17
00:00:39,530 --> 00:00:41,900
and it's equal to the curly braces,

18
00:00:41,900 --> 00:00:44,690
and inside it you've got your friend name, Rolf,

19
00:00:44,690 --> 00:00:48,300
and then a colon and then something like 24.

20
00:00:48,300 --> 00:00:51,010
This is a dictionary of a single element

21
00:00:51,010 --> 00:00:54,440
where the element is this key value pair.

22
00:00:54,440 --> 00:00:56,630
The pairing is done with the colon

23
00:00:56,630 --> 00:00:59,000
and that means that 24 is a value

24
00:00:59,000 --> 00:01:02,513
associated with they key, Rolf, which is a string.

25
00:01:03,500 --> 00:01:06,270
Keys must be strings, they can also be integers

26
00:01:06,270 --> 00:01:09,010
and any other hashable types.

27
00:01:09,010 --> 00:01:11,240
Now I know we haven't talked about hashable types yet,

28
00:01:11,240 --> 00:01:15,200
but for now just think of them as integers and strings.

29
00:01:15,200 --> 00:01:18,140
If we want multiple values in a single dictionary,

30
00:01:18,140 --> 00:01:21,080
you can separate the key value pairs with a comma.

31
00:01:21,080 --> 00:01:26,080
So we can have Adam, 30, and Anne, 27, for example.

32
00:01:26,360 --> 00:01:30,170
And here we have a dictionary with three values, three keys.

33
00:01:30,170 --> 00:01:32,370
Rolf, Adam, and Anne.

34
00:01:32,370 --> 00:01:35,400
And each of those keys has a value associated with them.

35
00:01:35,400 --> 00:01:38,914
Rolf has 24, Adam has 30, and Anne has 27.

36
00:01:38,914 --> 00:01:42,583
If you wanted to access one particular element,

37
00:01:42,583 --> 00:01:45,710
you can do friend underscore ages

38
00:01:45,710 --> 00:01:47,490
and then using subscript notation,

39
00:01:47,490 --> 00:01:49,250
access the appropriate key.

40
00:01:49,250 --> 00:01:51,917
So in here we would put, for example, Adam

41
00:01:51,917 --> 00:01:54,680
and then we would access the Adam key

42
00:01:54,680 --> 00:01:56,972
inside the friend ages dictionary.

43
00:01:56,972 --> 00:01:59,070
Remember that in lists and doubles

44
00:01:59,070 --> 00:02:00,450
you could use subscript notation

45
00:02:00,450 --> 00:02:03,960
to access a particular index such as zero, one, and two,

46
00:02:03,960 --> 00:02:05,680
but that won't work in dictionaries.

47
00:02:05,680 --> 00:02:08,278
You must access the keys themselves.

48
00:02:08,278 --> 00:02:10,527
Of course, if we printed this out,

49
00:02:10,527 --> 00:02:15,527 line:15% 
you'll see that we get 30 out, just there.

50
00:02:17,370 --> 00:02:21,010
If you wanted to change a particular element or add one,

51
00:02:21,010 --> 00:02:23,310
you can do that by doing friend ages,

52
00:02:23,310 --> 00:02:26,700
and then for example Bob equal 20.

53
00:02:26,700 --> 00:02:28,350
Then if you print friend ages,

54
00:02:28,350 --> 00:02:31,110 line:15% 
you'll see that Bob will have been added to the dictionary.

55
00:02:31,110 --> 00:02:34,620 line:15% 
So when we print it out, we've got Rolf, Adam, Anne, and Bob

56
00:02:34,620 --> 00:02:37,380
And if you wanna change one, just do the same thing,

57
00:02:37,380 --> 00:02:38,763
but with an existing key.

58
00:02:40,210 --> 00:02:42,890 line:15% 
And now Rolf is 20.

59
00:02:42,890 --> 00:02:44,650
So that is how you can add a key,

60
00:02:44,650 --> 00:02:46,680
just access it and set a value to it,

61
00:02:46,680 --> 00:02:48,303
or change an existing key.

62
00:02:50,000 --> 00:02:51,960
Usually when we are working with dictionaries that are used

63
00:02:51,960 --> 00:02:54,730
to represent multiple things in the real world,

64
00:02:54,730 --> 00:02:57,420
such as multiple friends or things like that,

65
00:02:57,420 --> 00:03:00,559
it's quite convenient to have a list of dictionaries.

66
00:03:00,559 --> 00:03:03,163
For example, let's make a list of our friends.

67
00:03:04,480 --> 00:03:06,840
Now in the past, we've had a list of friends,

68
00:03:06,840 --> 00:03:11,250
such as Rolf, Adam, and Anne like this,

69
00:03:11,250 --> 00:03:13,570
but you can actually have a list of dictionaries,

70
00:03:13,570 --> 00:03:16,270
each dictionary describing a friend.

71
00:03:16,270 --> 00:03:17,103
By the way,

72
00:03:17,103 --> 00:03:19,350
I'm not sure if we've looked at this split syntax for lists,

73
00:03:19,350 --> 00:03:21,340
but lists don't care about white space,

74
00:03:21,340 --> 00:03:23,130
so you can put multiple values in one line,

75
00:03:23,130 --> 00:03:24,880
or you can split them across several lines,

76
00:03:24,880 --> 00:03:25,763
however you like.

77
00:03:26,770 --> 00:03:29,880
Going back to our dictionary, you can, instead of Rolf,

78
00:03:29,880 --> 00:03:33,290
create a dictionary that contains name

79
00:03:33,290 --> 00:03:35,270
and Rolf associated with it,

80
00:03:35,270 --> 00:03:39,550
and age and 24 associated with it.

81
00:03:39,550 --> 00:03:41,644
Now if we do this for each of our friends,

82
00:03:41,644 --> 00:03:46,050
and I will do that with Adam and Anne,

83
00:03:46,050 --> 00:03:47,400
and their appropriate ages,

84
00:03:50,040 --> 00:03:52,040
That means that now what we've got

85
00:03:52,040 --> 00:03:53,680
is a list of dictionaries

86
00:03:53,680 --> 00:03:56,370
and each dictionary represent a friend.

87
00:03:56,370 --> 00:03:58,850
But that means that we can store more

88
00:03:58,850 --> 00:04:02,010
than just their name in each element.

89
00:04:02,010 --> 00:04:03,230
Before we were constrained

90
00:04:03,230 --> 00:04:05,290
to just storing their name in our list,

91
00:04:05,290 --> 00:04:07,703
but now we can store more data if we want.

92
00:04:09,750 --> 00:04:12,530
If you print friends out, you'll see that you get back

93
00:04:12,530 --> 00:04:15,500 line:15% 
your list of friends there.

94
00:04:15,500 --> 00:04:17,490
And of course if you wanted to print a particular friend,

95
00:04:17,490 --> 00:04:21,290
you would need to use the index, because this is a list.

96
00:04:21,290 --> 00:04:24,330
The friends list needs to be accessed by index

97
00:04:24,330 --> 00:04:26,620
so if you wanted to access index zero,

98
00:04:26,620 --> 00:04:28,580
that would give you back this dictionary.

99
00:04:28,580 --> 00:04:30,670
Index one would give you this dictionary.

100
00:04:30,670 --> 00:04:33,040
And index two would give you this dictionary.

101
00:04:33,040 --> 00:04:35,070
So let's say we want to access index one,

102
00:04:35,070 --> 00:04:36,820
which is this dictionary,

103
00:04:36,820 --> 00:04:38,080
if you print this out

104
00:04:38,080 --> 00:04:39,550 line:15% 
you'll see that what you get back is,

105
00:04:39,550 --> 00:04:41,990 line:15% 
naturally, the entire dictionary.

106
00:04:41,990 --> 00:04:46,360
That means that now, after friends one,

107
00:04:46,360 --> 00:04:50,150
you can put another subscript notation to access the name,

108
00:04:50,150 --> 00:04:51,570
for example.

109
00:04:51,570 --> 00:04:54,070 line:15% 
And doing this would give you Adam.

110
00:04:54,070 --> 00:04:57,300
So this is how you can access a list of dictionaries,

111
00:04:57,300 --> 00:05:00,540
or a particular value inside a list of dictionaries.

112
00:05:00,540 --> 00:05:02,220
If you had a list of lists,

113
00:05:02,220 --> 00:05:05,780
you would access multiple indices in succession like this.

114
00:05:05,780 --> 00:05:07,970
And if you had a dictionary of dictionaries,

115
00:05:07,970 --> 00:05:10,483
then you would access multiple keys in succession.

116
00:05:11,350 --> 00:05:14,030
Let me say of course that you can nest these lists

117
00:05:14,030 --> 00:05:17,030
and doubles and dictionaries however way you want in Python.

118
00:05:19,500 --> 00:05:21,220
Let's take a look at another example

119
00:05:21,220 --> 00:05:24,492
where we have a dictionary that maps the student names

120
00:05:24,492 --> 00:05:28,410
to the percentage of attendance they have to their lessons.

121
00:05:28,410 --> 00:05:31,140
So Rolf has attended 96% of his lessons.

122
00:05:31,140 --> 00:05:34,700
Bob 80% and Anne 100%.

123
00:05:34,700 --> 00:05:39,550
You can iterate over a dictionary, using a for loop.

124
00:05:39,550 --> 00:05:43,630
And what this does is it gives you the keys.

125
00:05:43,630 --> 00:05:45,210
So if you print this student variable

126
00:05:45,210 --> 00:05:46,870
that we're using here for iteration

127
00:05:46,870 --> 00:05:49,480 line:15% 
what you get back are the student names.

128
00:05:49,480 --> 00:05:51,463
The keys in your dictionary.

129
00:05:52,350 --> 00:05:55,883
Using that key, you can print something like this.

130
00:05:58,960 --> 00:06:01,780
Which will print out the student variable

131
00:06:01,780 --> 00:06:03,610
which is the students name

132
00:06:03,610 --> 00:06:06,570
and then you can access the student key

133
00:06:06,570 --> 00:06:08,100
in the student attendance dictionary

134
00:06:08,100 --> 00:06:11,400
using subscript notation to get their attendance.

135
00:06:11,400 --> 00:06:13,200
So if we save this and run it

136
00:06:13,200 --> 00:06:17,160 line:15% 
you now see that we get Rolf 96, Bob 80, Anne 100.

137
00:06:17,160 --> 00:06:20,291
You can of course put a percentage sign there if you like

138
00:06:20,291 --> 00:06:21,124
to make it a little bit nicer.

139
00:06:21,124 --> 00:06:23,820
However accessing a dictionary like this in a for loop

140
00:06:23,820 --> 00:06:27,130
doesn't look the best because there are other, better ways.

141
00:06:27,130 --> 00:06:32,040
The better way is if you do student attendance dot items

142
00:06:32,040 --> 00:06:36,467
and then here you can do for student comma attendance

143
00:06:36,467 --> 00:06:39,100
in student attendance dot items.

144
00:06:39,100 --> 00:06:41,960
And then here you no longer need to access dictionary

145
00:06:41,960 --> 00:06:44,940
because you've got it in the attendance variable.

146
00:06:44,940 --> 00:06:48,654
So what we've got here is that student attendance dot items

147
00:06:48,654 --> 00:06:51,610
gives you two values for each student

148
00:06:51,610 --> 00:06:55,590
that you can get as two separate variables here in Python.

149
00:06:55,590 --> 00:06:57,321
Saving and running this

150
00:06:57,321 --> 00:06:59,073 line:15% 
will give you exactly the same as before.

151
00:07:00,080 --> 00:07:02,520
You can use the in keyword in a dictionary

152
00:07:02,520 --> 00:07:05,530
to check whether a value is one of the keys

153
00:07:05,530 --> 00:07:06,363
of the dictionary.

154
00:07:06,363 --> 00:07:10,880
So for example, if Bob in student attendance,

155
00:07:10,880 --> 00:07:11,910
you can print something like

156
00:07:11,910 --> 00:07:14,733
Bob attended this many lectures.

157
00:07:16,450 --> 00:07:17,760
Otherwise, you can print that

158
00:07:17,760 --> 00:07:21,363
Bob is not a student in this class.

159
00:07:23,140 --> 00:07:25,450
So, the in keyword allows you to check

160
00:07:25,450 --> 00:07:27,820
whether a value is one of the keys.

161
00:07:27,820 --> 00:07:30,640
Notice that you can't check whether one of the values

162
00:07:30,640 --> 00:07:33,903
is in the dictionary, you can only check the keys.

163
00:07:35,110 --> 00:07:38,850
Finally, you can also get just the values back if you like.

164
00:07:38,850 --> 00:07:41,210
If you wanted 96, 80, and 100

165
00:07:41,210 --> 00:07:43,050
without worrying about the keys,

166
00:07:43,050 --> 00:07:46,030
for example if wanted to calculate an average,

167
00:07:46,030 --> 00:07:49,010
you could have a variable called attendance values

168
00:07:49,010 --> 00:07:53,080
and make it equal to student attendance dot values.

169
00:07:53,080 --> 00:07:56,042
And then you can print the sum of attendance values

170
00:07:56,042 --> 00:08:01,042
divided by the length of attendance values.

171
00:08:01,210 --> 00:08:02,700
And what this is going to give you is

172
00:08:02,700 --> 00:08:07,200
add 96, 80, and 100 together and divide by three

173
00:08:07,200 --> 00:08:10,860
which is the length of what you get back which is a list.

174
00:08:10,860 --> 00:08:13,493 line:15% 
So if you run that you get back 92.0.

175
00:08:15,050 --> 00:08:17,350
Going back to what we had earlier,

176
00:08:17,350 --> 00:08:20,940
which is this syntax where you have two variables

177
00:08:20,940 --> 00:08:23,860
being defined, separated by a comma inside a for loop,

178
00:08:23,860 --> 00:08:26,150
we're going to learn more about that in the next video.

179
00:08:26,150 --> 00:08:28,370
So if that's unclear, don't worry about it,

180
00:08:28,370 --> 00:08:29,570
we'll look at it just now.

181
00:08:29,570 --> 00:08:31,153
I'll see you in the next video.

