WEBVTT

0
00:00.450 --> 00:03.270
Now that you've seen how list comprehension works,

1
00:03.570 --> 00:07.290
it's time to learn about another type of comprehension, dictionary

2
00:07.290 --> 00:08.250
comprehensions.

3
00:08.820 --> 00:13.820
This is also super useful because it allows us to create a new dictionary from

4
00:14.940 --> 00:18.990
the values in a list or in a dictionary. For example,

5
00:19.050 --> 00:21.120
we could write out our keywords like this.

6
00:21.540 --> 00:24.180
We're going to create a new dictionary called new_dict,

7
00:24.600 --> 00:29.250
and it's going to be created using this curly bracket now instead of square

8
00:29.250 --> 00:33.420
brackets. And inside the comprehension, we get to set the new_key,

9
00:33.750 --> 00:38.190
the new_value, and we get to loop through items in a list.

10
00:38.430 --> 00:42.480
So any sort of iterable; a list, a range, a string, anything you want.

11
00:42.810 --> 00:45.810
And this is the simplest form of dictionary comprehension.

12
00:46.380 --> 00:51.380
Now remember that a dictionary comprehension is just a way of creating a

13
00:51.690 --> 00:54.540
dictionary using this shortened syntax.

14
00:55.020 --> 00:57.150
We could take this one step further.

15
00:57.570 --> 01:02.570
We could also create a new dictionary based on the values in an existing

16
01:03.600 --> 01:04.433
dictionary.

17
01:04.710 --> 01:09.710
So we could take that dictionary and then get hold of all of the items inside

18
01:10.080 --> 01:14.760
that dictionary, and then split it into a key and a value.

19
01:15.210 --> 01:19.680
So now, we're looping through each of the keys and each of the values in all of

20
01:19.680 --> 01:21.300
the items from the dictionary,

21
01:21.840 --> 01:26.840
and we can use these key and value variables to create a new_key or a new_value.

22
01:29.280 --> 01:33.300
And to go one step further for completion sake, we can, of course,

23
01:33.480 --> 01:36.570
as always add our condition at the very end,

24
01:36.690 --> 01:39.450
just like we did before with list comprehensions.

25
01:40.050 --> 01:42.450
So let's see how this code works in practice.

26
01:42.780 --> 01:44.790
I'm going to take my previous list of names

27
01:44.940 --> 01:49.920
and then I'm going to click on this button to refresh my console so that all of

28
01:49.920 --> 01:51.720
the previous variables get deleted.

29
01:52.410 --> 01:56.400
So now I'm going to create a new list of names.

30
01:57.120 --> 02:00.660
So I'm going to use the previous names I had before, and this is our list here.

31
02:01.260 --> 02:05.400
Now, the next thing I'm going to do is I'm going to create a dictionary where I

32
02:05.400 --> 02:09.510
generate a random score for each of these names.

33
02:09.780 --> 02:14.780
So let's imagine we had a bunch of students and we wanted to create some random

34
02:15.060 --> 02:19.170
scores for each of them so that they would get a random score between 1 and

35
02:19.170 --> 02:22.740
100. Now for the teachers out there,

36
02:22.740 --> 02:26.310
I really hope this is not what you do when you generate the students' scores.

37
02:26.850 --> 02:30.450
But essentially we want to create a dictionary

38
02:30.450 --> 02:35.430
which looks something like this. Each of the students in this list,

39
02:35.550 --> 02:40.140
so let's start out with Alex. We want our dictionary to have a key,

40
02:40.170 --> 02:43.080
which is the name of the student, and then a value,

41
02:43.110 --> 02:45.240
which is their random score.

42
02:45.750 --> 02:50.640
And we want to continue through that list of names until we've generated a

43
02:50.640 --> 02:53.970
random score for each of them. That's the goal.

44
02:54.090 --> 02:57.300
And now we have to use our dictionary comprehension.

45
02:57.930 --> 03:01.540
Remember, when we're creating a new list using lists comprehension,

46
03:01.570 --> 03:04.390
we're using square brackets. But now we're creating a dictionary

47
03:04.390 --> 03:08.080
so we're going to use curly brackets. Now, inside these curly brackets,

48
03:08.110 --> 03:10.750
I'm going to put down my usual list of keywords.

49
03:10.840 --> 03:13.900
So it's going to be a new_key:

50
03:13.960 --> 03:17.620
new_value that we're going to be adding to our new dictionary.

51
03:18.160 --> 03:19.480
And then I'm going to do

52
03:19.480 --> 03:23.770
a for item in list. In this case,

53
03:23.800 --> 03:28.090
our list or our iterable is going to be all lists of names.

54
03:28.810 --> 03:31.630
Now, even though in my keywords I always use lists,

55
03:31.870 --> 03:34.450
it's just easier to imagine. But it's actually, in fact,

56
03:34.450 --> 03:38.320
any sort of iterable like a range or a string or a tuple.

57
03:38.890 --> 03:40.990
Once we've got our list of names,

58
03:41.020 --> 03:43.450
we're going to name each of those names something,

59
03:43.510 --> 03:45.220
and I'm just going to call it student.

60
03:45.700 --> 03:49.900
So we're looping through all the students in our list of names here.

61
03:50.470 --> 03:52.300
And for each of those students,

62
03:52.330 --> 03:56.050
I'm going to create the student as the new key,

63
03:56.650 --> 04:00.610
but the value is going to be a new, random number.

64
04:01.030 --> 04:02.830
So in order to create a random number,

65
04:02.830 --> 04:06.370
I'm going to have to first import the random module.

66
04:06.760 --> 04:10.150
So I've cut out my previous code, imported random,

67
04:10.270 --> 04:11.860
and let's paste that code back in.

68
04:12.340 --> 04:17.340
So now we can replace the value for each of these dictionary entries with random

69
04:18.070 --> 04:19.270
.randint,

70
04:19.390 --> 04:23.530
and we can generate a random number between 1 and 100.

71
04:24.040 --> 04:28.240
So now when I hit enter, you'll see our new students_scores

72
04:28.240 --> 04:29.620
dictionary being created

73
04:29.950 --> 04:34.360
and you can see we've got all of our students in our list of names, Alex, Beth,

74
04:34.360 --> 04:35.710
Caroline, et cetera.

75
04:35.980 --> 04:40.060
And each of them now have a new random score.

76
04:40.810 --> 04:44.710
So it seems like everybody did pretty well other than Eleanor,

77
04:44.860 --> 04:49.450
I guess. Random score generating seems to work. Now,

78
04:49.480 --> 04:50.320
the next step,

79
04:50.350 --> 04:53.260
we're going to take this a little bit further because this was quite simple.

80
04:53.260 --> 04:57.190
We just did what we did before, which is looping through a list.

81
04:57.550 --> 05:01.810
But now I want to loop through a dictionary. Luckily,

82
05:01.810 --> 05:06.580
we've now created this brand, sparkling, new dictionary, our students_scores,

83
05:07.030 --> 05:11.050
and I want to use that dictionary in my next dictionary comprehension.

84
05:11.620 --> 05:15.640
So the next thing I want to do is to be able to create a dictionary called

85
05:15.700 --> 05:19.750
passed_students. And this is going to be a dictionary

86
05:19.780 --> 05:22.900
which looks through the dictionary of student_scores,

87
05:22.900 --> 05:26.320
so imagine you have an entire high school where everybody's scores are logged

88
05:26.710 --> 05:30.820
and you look at everybody who's got a score of 60 or over.

89
05:31.210 --> 05:32.080
And then we're going to say, well,

90
05:32.080 --> 05:35.920
those people have passed and everybody else has failed that year.

91
05:36.640 --> 05:41.350
Essentially we should loop through this entire dictionary of items,

92
05:41.620 --> 05:46.030
figure out which of these have a value that's equal to 60 or over 60,

93
05:46.540 --> 05:51.070
and then we're gonna add those items back into this new dictionary

94
05:51.130 --> 05:52.450
of passed_students.

95
05:53.620 --> 05:58.550
So essentially, we'd probably end up with, um, Beth,

96
05:58.580 --> 06:02.000
with Caroline, with Dave and with Freddy.

97
06:02.510 --> 06:07.510
So it will still look pretty much the same as our students_scores dictionary.

98
06:08.120 --> 06:11.870
So it would have something like Beth and then colon her score,

99
06:11.900 --> 06:16.550
which is 72. But just the fact that she's included in this dictionary,

100
06:16.580 --> 06:17.900
it means that she has passed.

101
06:18.650 --> 06:23.650
And we will end up with this new dictionary where everybody is a passed student

102
06:24.260 --> 06:25.580
and bear scores.

103
06:27.140 --> 06:31.700
Have a think about how you might do this. And if you're up for the challenge,

104
06:31.760 --> 06:35.120
try pausing the video and seeing if you can complete this.

105
06:37.250 --> 06:41.900
As always, we know that we're going to create a new dictionary, so curly braces,

106
06:42.260 --> 06:46.100
and then it's going to be our new_key: new_value.

107
06:46.670 --> 06:50.180
And then it's going to be for, and now,

108
06:50.210 --> 06:55.210
because we are looping through this dictionary and we want to get hold of each

109
06:55.490 --> 06:56.323
of the values,

110
06:56.660 --> 07:00.980
we're going to use the method that we saw in our previous slides.

111
07:01.160 --> 07:06.160
So we're going to get hold of each of the keys and the value from our

112
07:07.040 --> 07:09.890
dictionary. And then after the in keyword,

113
07:09.920 --> 07:13.760
it's the thing that we actually want to loop through, which is our dictionary.

114
07:15.260 --> 07:16.700
Once we've got our dictionary,

115
07:16.700 --> 07:20.210
then we need to call the items method on that dictionary.

116
07:20.210 --> 07:23.000
So it's items with a set of parentheses,

117
07:23.060 --> 07:25.490
not just the items as an attribute.

118
07:26.150 --> 07:28.340
So now that we've got our keywords down,

119
07:28.340 --> 07:30.950
let's convert it to our particular case.

120
07:31.370 --> 07:35.210
So our dictionary is called students_scores,

121
07:35.420 --> 07:37.070
let's make sure I spell it correctly.

122
07:37.400 --> 07:42.140
So students_scores.items gets all of the items in that dictionary.

123
07:42.650 --> 07:44.360
And then for each of the keys

124
07:44.420 --> 07:47.750
which is going to be students, and for each of the values

125
07:47.750 --> 07:52.610
which is going to be their score, we're going to loop through this dictionary.

126
07:53.180 --> 07:58.130
The new key is just going to be the student's name and the new value is just

127
07:58.130 --> 07:59.510
going to be their score.

128
08:00.050 --> 08:05.050
But what we need to do is we need to check using an if statement,

129
08:05.750 --> 08:09.530
if the student that we're currently looping through has passed.

130
08:09.980 --> 08:14.980
So the test is basically looking at the score and seeing if it is greater than

131
08:15.920 --> 08:19.820
or equal to 60. And if that is the case,

132
08:19.850 --> 08:22.910
then we're going to take this student as the new key,

133
08:23.150 --> 08:27.860
their score as the new value, and place it inside this new dictionary.

134
08:28.550 --> 08:31.280
So now if I go ahead and hit enter,

135
08:31.640 --> 08:35.480
then you can see we've got our new dictionary being created,

136
08:35.900 --> 08:40.130
which only contains all the students who have passed. So it's Beth, Caroline,

137
08:40.160 --> 08:43.700
Dave, and Freddy, and we've lost Alex and Eleanor.

138
08:45.500 --> 08:47.480
So in order to get more practice at this,

139
08:47.570 --> 08:52.250
I've prepared some more coding exercises for you on dictionary comprehension.

140
08:52.550 --> 08:55.250
So head over to the next lesson and we'll get started.