1
00:00:00,450 --> 00:00:03,270
Now that you've seen how list comprehension works,

2
00:00:03,570 --> 00:00:07,290
it's time to learn about another type of comprehension, dictionary

3
00:00:07,290 --> 00:00:08,250
comprehensions.

4
00:00:08,820 --> 00:00:13,820
This is also super useful because it allows us to create a new dictionary from

5
00:00:14,940 --> 00:00:18,990
the values in a list or in a dictionary. For example,

6
00:00:19,050 --> 00:00:21,120
we could write out our keywords like this.

7
00:00:21,540 --> 00:00:24,180
We're going to create a new dictionary called new_dict,

8
00:00:24,600 --> 00:00:29,250
and it's going to be created using this curly bracket now instead of square

9
00:00:29,250 --> 00:00:33,420
brackets. And inside the comprehension, we get to set the new_key,

10
00:00:33,750 --> 00:00:38,190
the new_value, and we get to loop through items in a list.

11
00:00:38,430 --> 00:00:42,480
So any sort of iterable; a list, a range, a string, anything you want.

12
00:00:42,810 --> 00:00:45,810
And this is the simplest form of dictionary comprehension.

13
00:00:46,380 --> 00:00:51,380
Now remember that a dictionary comprehension is just a way of creating a

14
00:00:51,690 --> 00:00:54,540
dictionary using this shortened syntax.

15
00:00:55,020 --> 00:00:57,150
We could take this one step further.

16
00:00:57,570 --> 00:01:02,570
We could also create a new dictionary based on the values in an existing

17
00:01:03,600 --> 00:01:04,433
dictionary.

18
00:01:04,709 --> 00:01:09,710
So we could take that dictionary and then get hold of all of the items inside

19
00:01:10,080 --> 00:01:14,760
that dictionary, and then split it into a key and a value.

20
00:01:15,210 --> 00:01:19,680
So now, we're looping through each of the keys and each of the values in all of

21
00:01:19,680 --> 00:01:21,300
the items from the dictionary,

22
00:01:21,840 --> 00:01:26,840
and we can use these key and value variables to create a new_key or a new_value.

23
00:01:29,280 --> 00:01:33,300
And to go one step further for completion sake, we can, of course,

24
00:01:33,480 --> 00:01:36,570
as always add our condition at the very end,

25
00:01:36,690 --> 00:01:39,450
just like we did before with list comprehensions.

26
00:01:40,050 --> 00:01:42,450
So let's see how this code works in practice.

27
00:01:42,780 --> 00:01:44,790
I'm going to take my previous list of names

28
00:01:44,940 --> 00:01:49,920
and then I'm going to click on this button to refresh my console so that all of

29
00:01:49,920 --> 00:01:51,720
the previous variables get deleted.

30
00:01:52,410 --> 00:01:56,400
So now I'm going to create a new list of names.

31
00:01:57,120 --> 00:02:00,660
So I'm going to use the previous names I had before, and this is our list here.

32
00:02:01,260 --> 00:02:05,400
Now, the next thing I'm going to do is I'm going to create a dictionary where I

33
00:02:05,400 --> 00:02:09,509
generate a random score for each of these names.

34
00:02:09,780 --> 00:02:14,780
So let's imagine we had a bunch of students and we wanted to create some random

35
00:02:15,060 --> 00:02:19,170
scores for each of them so that they would get a random score between 1 and

36
00:02:19,170 --> 00:02:22,740
100. Now for the teachers out there,

37
00:02:22,740 --> 00:02:26,310
I really hope this is not what you do when you generate the students' scores.

38
00:02:26,850 --> 00:02:30,450
But essentially we want to create a dictionary

39
00:02:30,450 --> 00:02:35,430
which looks something like this. Each of the students in this list,

40
00:02:35,550 --> 00:02:40,140
so let's start out with Alex. We want our dictionary to have a key,

41
00:02:40,170 --> 00:02:43,080
which is the name of the student, and then a value,

42
00:02:43,110 --> 00:02:45,240
which is their random score.

43
00:02:45,750 --> 00:02:50,640
And we want to continue through that list of names until we've generated a

44
00:02:50,640 --> 00:02:53,970
random score for each of them. That's the goal.

45
00:02:54,090 --> 00:02:57,300
And now we have to use our dictionary comprehension.

46
00:02:57,930 --> 00:03:01,540
Remember, when we're creating a new list using lists comprehension,

47
00:03:01,570 --> 00:03:04,390
we're using square brackets. But now we're creating a dictionary

48
00:03:04,390 --> 00:03:08,080
so we're going to use curly brackets. Now, inside these curly brackets,

49
00:03:08,110 --> 00:03:10,750
I'm going to put down my usual list of keywords.

50
00:03:10,840 --> 00:03:13,900
So it's going to be a new_key:

51
00:03:13,960 --> 00:03:17,620
new_value that we're going to be adding to our new dictionary.

52
00:03:18,160 --> 00:03:19,480
And then I'm going to do

53
00:03:19,480 --> 00:03:23,770
a for item in list. In this case,

54
00:03:23,800 --> 00:03:28,090
our list or our iterable is going to be all lists of names.

55
00:03:28,810 --> 00:03:31,630
Now, even though in my keywords I always use lists,

56
00:03:31,870 --> 00:03:34,450
it's just easier to imagine. But it's actually, in fact,

57
00:03:34,450 --> 00:03:38,320
any sort of iterable like a range or a string or a tuple.

58
00:03:38,890 --> 00:03:40,990
Once we've got our list of names,

59
00:03:41,020 --> 00:03:43,450
we're going to name each of those names something,

60
00:03:43,510 --> 00:03:45,220
and I'm just going to call it student.

61
00:03:45,700 --> 00:03:49,900
So we're looping through all the students in our list of names here.

62
00:03:50,470 --> 00:03:52,300
And for each of those students,

63
00:03:52,330 --> 00:03:56,050
I'm going to create the student as the new key,

64
00:03:56,650 --> 00:04:00,610
but the value is going to be a new, random number.

65
00:04:01,030 --> 00:04:02,830
So in order to create a random number,

66
00:04:02,830 --> 00:04:06,370
I'm going to have to first import the random module.

67
00:04:06,760 --> 00:04:10,150
So I've cut out my previous code, imported random,

68
00:04:10,270 --> 00:04:11,860
and let's paste that code back in.

69
00:04:12,340 --> 00:04:17,339
So now we can replace the value for each of these dictionary entries with random

70
00:04:18,070 --> 00:04:19,269
.randint,

71
00:04:19,390 --> 00:04:23,530
and we can generate a random number between 1 and 100.

72
00:04:24,040 --> 00:04:28,240
So now when I hit enter, you'll see our new students_scores

73
00:04:28,240 --> 00:04:29,620
dictionary being created

74
00:04:29,950 --> 00:04:34,360
and you can see we've got all of our students in our list of names, Alex, Beth,

75
00:04:34,360 --> 00:04:35,710
Caroline, et cetera.

76
00:04:35,980 --> 00:04:40,060
And each of them now have a new random score.

77
00:04:40,810 --> 00:04:44,710
So it seems like everybody did pretty well other than Eleanor,

78
00:04:44,860 --> 00:04:49,450
I guess. Random score generating seems to work. Now,

79
00:04:49,480 --> 00:04:50,320
the next step,

80
00:04:50,350 --> 00:04:53,260
we're going to take this a little bit further because this was quite simple.

81
00:04:53,260 --> 00:04:57,190
We just did what we did before, which is looping through a list.

82
00:04:57,550 --> 00:05:01,810
But now I want to loop through a dictionary. Luckily,

83
00:05:01,810 --> 00:05:06,580
we've now created this brand, sparkling, new dictionary, our students_scores,

84
00:05:07,030 --> 00:05:11,050
and I want to use that dictionary in my next dictionary comprehension.

85
00:05:11,620 --> 00:05:15,640
So the next thing I want to do is to be able to create a dictionary called

86
00:05:15,700 --> 00:05:19,750
passed_students. And this is going to be a dictionary

87
00:05:19,780 --> 00:05:22,900
which looks through the dictionary of student_scores,

88
00:05:22,900 --> 00:05:26,320
so imagine you have an entire high school where everybody's scores are logged

89
00:05:26,710 --> 00:05:30,820
and you look at everybody who's got a score of 60 or over.

90
00:05:31,210 --> 00:05:32,080
And then we're going to say, well,

91
00:05:32,080 --> 00:05:35,920
those people have passed and everybody else has failed that year.

92
00:05:36,640 --> 00:05:41,350
Essentially we should loop through this entire dictionary of items,

93
00:05:41,620 --> 00:05:46,030
figure out which of these have a value that's equal to 60 or over 60,

94
00:05:46,540 --> 00:05:51,070
and then we're gonna add those items back into this new dictionary

95
00:05:51,130 --> 00:05:52,450
of passed_students.

96
00:05:53,620 --> 00:05:58,550
So essentially, we'd probably end up with, um, Beth,

97
00:05:58,580 --> 00:06:02,000
with Caroline, with Dave and with Freddy.

98
00:06:02,510 --> 00:06:07,510
So it will still look pretty much the same as our students_scores dictionary.

99
00:06:08,120 --> 00:06:11,870
So it would have something like Beth and then colon her score,

100
00:06:11,900 --> 00:06:16,550
which is 72. But just the fact that she's included in this dictionary,

101
00:06:16,580 --> 00:06:17,900
it means that she has passed.

102
00:06:18,650 --> 00:06:23,650
And we will end up with this new dictionary where everybody is a passed student

103
00:06:24,260 --> 00:06:25,580
and bear scores.

104
00:06:27,140 --> 00:06:31,700
Have a think about how you might do this. And if you're up for the challenge,

105
00:06:31,760 --> 00:06:35,120
try pausing the video and seeing if you can complete this.

106
00:06:37,250 --> 00:06:41,900
As always, we know that we're going to create a new dictionary, so curly braces,

107
00:06:42,260 --> 00:06:46,100
and then it's going to be our new_key: new_value.

108
00:06:46,670 --> 00:06:50,180
And then it's going to be for, and now,

109
00:06:50,210 --> 00:06:55,210
because we are looping through this dictionary and we want to get hold of each

110
00:06:55,490 --> 00:06:56,323
of the values,

111
00:06:56,660 --> 00:07:00,980
we're going to use the method that we saw in our previous slides.

112
00:07:01,160 --> 00:07:06,160
So we're going to get hold of each of the keys and the value from our

113
00:07:07,040 --> 00:07:09,890
dictionary. And then after the in keyword,

114
00:07:09,920 --> 00:07:13,760
it's the thing that we actually want to loop through, which is our dictionary.

115
00:07:15,260 --> 00:07:16,700
Once we've got our dictionary,

116
00:07:16,700 --> 00:07:20,210
then we need to call the items method on that dictionary.

117
00:07:20,210 --> 00:07:23,000
So it's items with a set of parentheses,

118
00:07:23,060 --> 00:07:25,490
not just the items as an attribute.

119
00:07:26,150 --> 00:07:28,340
So now that we've got our keywords down,

120
00:07:28,340 --> 00:07:30,950
let's convert it to our particular case.

121
00:07:31,370 --> 00:07:35,210
So our dictionary is called students_scores,

122
00:07:35,420 --> 00:07:37,070
let's make sure I spell it correctly.

123
00:07:37,400 --> 00:07:42,140
So students_scores.items gets all of the items in that dictionary.

124
00:07:42,650 --> 00:07:44,360
And then for each of the keys

125
00:07:44,420 --> 00:07:47,750
which is going to be students, and for each of the values

126
00:07:47,750 --> 00:07:52,610
which is going to be their score, we're going to loop through this dictionary.

127
00:07:53,180 --> 00:07:58,130
The new key is just going to be the student's name and the new value is just

128
00:07:58,130 --> 00:07:59,510
going to be their score.

129
00:08:00,050 --> 00:08:05,050
But what we need to do is we need to check using an if statement,

130
00:08:05,750 --> 00:08:09,530
if the student that we're currently looping through has passed.

131
00:08:09,980 --> 00:08:14,980
So the test is basically looking at the score and seeing if it is greater than

132
00:08:15,920 --> 00:08:19,820
or equal to 60. And if that is the case,

133
00:08:19,850 --> 00:08:22,910
then we're going to take this student as the new key,

134
00:08:23,150 --> 00:08:27,860
their score as the new value, and place it inside this new dictionary.

135
00:08:28,550 --> 00:08:31,280
So now if I go ahead and hit enter,

136
00:08:31,640 --> 00:08:35,480
then you can see we've got our new dictionary being created,

137
00:08:35,900 --> 00:08:40,130
which only contains all the students who have passed. So it's Beth, Caroline,

138
00:08:40,159 --> 00:08:43,700
Dave, and Freddy, and we've lost Alex and Eleanor.

139
00:08:45,500 --> 00:08:47,480
So in order to get more practice at this,

140
00:08:47,570 --> 00:08:52,250
I've prepared some more coding exercises for you on dictionary comprehension.

141
00:08:52,550 --> 00:08:55,250
So head over to the next lesson and we'll get started.

