WEBVTT

0
00:00.260 --> 00:07.130
The big topic that we're going to explore today is a concept known as a List Comprehension.

1
00:07.130 --> 00:12.020
And as I mentioned before, this is something that's really unique to the Python language.

2
00:12.020 --> 00:17.480
In many other programming languages, you don't actually have access to something like this.

3
00:17.480 --> 00:19.610
There's nothing that really compares to it.

4
00:19.610 --> 00:25.040
And it's something that Python developers really, really love because it cuts down on the amount of

5
00:25.040 --> 00:31.090
typing, and it just makes the code a lot shorter and in most cases a lot easier to read.

6
00:31.240 --> 00:34.570
So what exactly is a List Comprehension?

7
00:34.600 --> 00:38.950
Well, it's a case where you create a new list from a previous list.

8
00:38.980 --> 00:41.920
So far we've been doing that using for loops.

9
00:41.920 --> 00:48.940
So for example, if I have a list of numbers [1, 2, 3] and I want to create a new list where

10
00:48.940 --> 00:56.550
each number is increased by one, well then I would use a for loop, creating a new empty list and then

11
00:56.550 --> 01:02.910
looping through each of the numbers in that list in order to add one to each of those numbers.

12
01:02.910 --> 01:10.500
And then I would append each of those increased numbers to my new_list, and I would end up with a

13
01:10.500 --> 01:13.170
new_list that consists of three items.

14
01:13.170 --> 01:15.570
And it will be [2, 3, 4].

15
01:15.720 --> 01:22.770
Now using list comprehension we can take these four lines of code and turn it into one.

16
01:22.770 --> 01:27.960
And the way that we create a list comprehension looks something like this.

17
01:27.990 --> 01:34.650
I like to use the keyword 'method', where you type out the list comprehension keywords, and then replace

18
01:34.650 --> 01:38.610
each of the words with the actual item in your code.

19
01:38.880 --> 01:45.400
The way that we would create any list comprehension is we would create the name of the new_list, and

20
01:45.400 --> 01:51.280
instead of creating an empty list and then having to append to it or populate it, we're actually going

21
01:51.280 --> 01:53.560
to create it straight in the same line.

22
01:53.560 --> 01:58.330
So we open up a set of square brackets to denote that we're creating a new list,

23
01:58.330 --> 02:04.510
and then we have this, "new_item for item in list" keyword pattern.

24
02:04.660 --> 02:06.010
Here's how it would work.

25
02:06.010 --> 02:11.520
Let's take that same thing that we did before, where we took a list of numbers, and we created a new

26
02:11.520 --> 02:14.340
list where each number is increased by one.

27
02:14.340 --> 02:17.430
And let's see how we would do it using list comprehension.

28
02:18.330 --> 02:22.080
So the first keyword I'm going to replace is the list.

29
02:22.080 --> 02:24.990
So this is the list that we're going to iterate through,

30
02:24.990 --> 02:28.320
and in our case it's this list of numbers.

31
02:28.320 --> 02:33.650
So let's replace that list keyword with the actual list, which is numbers.

32
02:33.740 --> 02:38.180
The next thing that is easy to understand is each item in the list.

33
02:38.180 --> 02:42.800
So we can call this anything we want, in the for loop here we called it n,

34
02:42.800 --> 02:46.430
so why don't we go ahead and call it n again.

35
02:47.600 --> 02:54.350
This n is going to represent each of the numbers in that list of numbers, just as it does in this for

36
02:54.350 --> 02:59.750
loop and the final keyword that we haven't yet replaced is the new_item.

37
02:59.750 --> 03:04.430
So what do we want each of the new items to be in this new list?

38
03:04.430 --> 03:11.210
Well, each of the new items is basically calculated by taking n and then adding 1 to it.

39
03:11.210 --> 03:19.610
So this line is the expression or the code that we need to execute in order to get a new_item.

40
03:19.610 --> 03:23.040
So this becomes our final list comprehension.

41
03:23.040 --> 03:24.990
We have our list of numbers,

42
03:24.990 --> 03:31.830
and then we create a new_list where we loop through each of the numbers n inside this list of numbers,

43
03:31.830 --> 03:37.260
and then for each of the n, we add one to it in order to create the final list.

44
03:37.260 --> 03:43.500
So when you look at a list comprehension just like this, as in the real code, it can be a little bit

45
03:43.500 --> 03:44.610
confusing to see.

46
03:44.610 --> 03:45.680
How would you read it?

47
03:45.680 --> 03:47.390
Which order does it happen?

48
03:47.390 --> 03:53.300
But when you're using this keyword method that I often use, where you just type out what you need to

49
03:53.300 --> 03:56.750
have inside a list comprehension, you need a name for the new list.

50
03:56.750 --> 03:58.460
You need a set of square brackets.

51
03:58.460 --> 04:04.040
And then inside the square brackets, you have, "new_item for item in list," and then you go through it

52
04:04.040 --> 04:08.390
replacing each of the keywords list, item and new_item.

53
04:08.870 --> 04:12.500
So I want you to have a go at actually writing out this code.

54
04:12.500 --> 04:15.410
Go ahead and create a new PyCharm project.

55
04:15.410 --> 04:18.380
And I want you to go into the Python console.

56
04:18.380 --> 04:22.040
We're not actually going to be writing code inside our main.py,

57
04:22.040 --> 04:28.250
and the reason is because the console allows us to write code line by line, and then we can view our

58
04:28.250 --> 04:31.580
variables really easily on the right hand side pane here.

59
04:32.270 --> 04:38.440
For example, if I have my list of numbers [1, 2, 3] and then I have to write out my list

60
04:38.440 --> 04:42.790
comprehension where we create our new_list, which I'll call new_numbers.

61
04:42.790 --> 04:46.510
And then we have to create our list comprehension.

62
04:46.510 --> 04:52.360
So the way to remember it is, "new_item for item in list."

63
04:52.360 --> 05:00.330
So now go ahead and write this out and see if you can remember how we actually structure our list comprehension,

64
05:00.330 --> 05:04.260
and see if you can get it to work within your PyCharm console.

65
05:04.560 --> 05:05.670
So pause the video.

66
05:05.670 --> 05:06.570
Give that a go.

67
05:08.010 --> 05:08.490
All right.

68
05:08.490 --> 05:11.430
So our list is of course this list of numbers.

69
05:11.430 --> 05:13.320
So let's replace that first.

70
05:13.320 --> 05:18.390
And then for each of the items in that list of numbers, well we can call it anything we want.

71
05:18.390 --> 05:21.210
So I'm just going to call it n or num,

72
05:21.210 --> 05:22.140
it doesn't matter.

73
05:22.140 --> 05:28.970
But as long as you're consistent in using it when you create the new_item, because each new_item is

74
05:28.970 --> 05:34.460
going to be the n, which is each number in the list + 1.

75
05:34.460 --> 05:41.360
So now when I hit Enter, you can see I've got this new list called new_numbers created from the previous

76
05:41.360 --> 05:42.620
list numbers,

77
05:42.620 --> 05:48.340
and this now has the same number of items, but each item is now increased by one.

78
05:49.210 --> 05:55.060
Now it's important to remember that when we say list comprehension, it doesn't strictly mean that you

79
05:55.060 --> 05:56.710
can only work with lists,

80
05:56.710 --> 06:00.760
you can work with other sequences like strings, for example.

81
06:00.760 --> 06:04.810
So here I've got a variable called name which is just a string.

82
06:04.810 --> 06:06.400
It's the word, Angela.

83
06:06.400 --> 06:14.040
And I'm creating a new_list by using a list comprehension that has this kind of code.

84
06:14.070 --> 06:19.560
Now, I want you to pause for a second and think about what would be in this new_list.

85
06:19.560 --> 06:21.120
What would it look like?

86
06:21.120 --> 06:24.180
And then I want you to try it out inside PyCharm.

87
06:24.180 --> 06:25.680
So pause the video now.

88
06:27.870 --> 06:28.410
All right.

89
06:28.410 --> 06:30.420
So, name = "Angela".

90
06:30.450 --> 06:33.180
This is the variable that we're working with,

91
06:33.180 --> 06:39.530
and then I'm going to create a new_list which I'll call letters_list.

92
06:40.280 --> 06:44.720
This new list is going to be created using my list comprehension.

93
06:44.720 --> 06:50.660
So it's going to be, "letter for letter in name."

94
06:50.660 --> 06:56.510
And what this does is it takes this sequence, this string,

95
06:56.510 --> 06:59.900
it goes through each of the letters in that string,

96
06:59.900 --> 07:04.780
and then it adds each of the letters into this new_list.

97
07:04.780 --> 07:08.800
So when I hit Enter, you can see this is what letters_list looks like.

98
07:08.800 --> 07:16.090
It's just split off my string into individual letters, and it's added it into a brand new list.

99
07:17.080 --> 07:26.370
As I mentioned, these things in Python, like a list, or a string, or a range, or tuple, they're called

100
07:26.370 --> 07:29.790
sequences because they have a specific order,

101
07:29.790 --> 07:35.640
and when you perform a list comprehension, it's going to take that sequence and it's going to go through

102
07:35.670 --> 07:42.990
it in order, either be it the letters in the string, or the items in a list, and then it's going to

103
07:42.990 --> 07:48.060
take each of those items in that correct order and then do something with it.

104
07:48.060 --> 07:52.910
Either add 1 to it or in this case, do nothing and just add it to a new list.

105
07:53.450 --> 07:54.770
Here's a challenge for you.

106
07:54.800 --> 08:01.610
Can you take the range, which is also a sequence that we can iterate through, and then create a range

107
08:01.610 --> 08:03.590
between 1 and 5?

108
08:03.590 --> 08:09.200
And remember that the way that the range works is it's going to take 1, and then 2 and then 3

109
08:09.200 --> 08:12.770
and then 4, but it's not going to go up to 5.

110
08:12.860 --> 08:19.180
I want you to loop through this range and then create a list where each of the numbers in the range

111
08:19.180 --> 08:24.010
is doubled, and the final list should look like this,

112
08:24.040 --> 08:26.740
it'll be [2, 4, 6, 8].

113
08:26.770 --> 08:29.920
Pause the video and see if you can complete this challenge.

114
08:32.230 --> 08:32.740
All right.

115
08:32.740 --> 08:37.120
So let's create our new list which I'm just going to call range_list,

116
08:37.120 --> 08:40.380
and we're going to use that keyword pattern to remind ourselves.

117
08:40.380 --> 08:44.730
So it's going to be, "new_item for item in list."

118
08:44.730 --> 08:48.360
And this list in this case, is actually not going to be a list,

119
08:48.360 --> 08:51.000
it's going to be a range between 1 and 5.

120
08:51.000 --> 08:55.380
And then each of the items we get to give it a name, which I'll just call num.

121
08:55.380 --> 09:00.030
And then in this new list, what is each new_item going to be?

122
09:00.030 --> 09:03.390
Well, it's going to be num * two.

123
09:03.390 --> 09:08.720
So now when I hit Enter, I get my range list, which is [2, 4, 6, 8].

124
09:08.720 --> 09:13.970
So it's looped through each of the numbers in the range, and it's multiplied each of those numbers by

125
09:13.970 --> 09:14.450
2,

126
09:14.450 --> 09:18.860
and then it's created a new list using each of those numbers.

127
09:19.610 --> 09:25.760
Now the final thing I want to show you regarding list comprehensions is that there's also such a thing

128
09:25.760 --> 09:28.750
as a Conditional List Comprehension.

129
09:28.780 --> 09:35.200
This takes our keywords a little bit further, whereas previously we stopped right here,

130
09:35.830 --> 09:38.080
"new_item for item in list,"

131
09:38.080 --> 09:42.580
we can also tag on two more keywords 'if' and a 'test'.

132
09:42.850 --> 09:51.430
What this is going to allow us to do is to only add this new item, and only to perform this code if

133
09:51.430 --> 09:53.880
the test actually passes.

134
09:54.720 --> 09:56.100
Here's an example.

135
09:56.100 --> 10:01.560
I've got a bunch of names here ["Alex", "Beth", "Caroline", "Dave", "Elanor", "Freddie"].

136
10:01.590 --> 10:08.070
Now, one of the neat things that you can actually do with the Python console is not only can you view

137
10:08.070 --> 10:12.720
what all of the variables are equal to, but you can also edit them if you have an issue.

138
10:12.750 --> 10:17.870
The sharp-eyed amongst you will know that I've actually made a typo in this name.

139
10:17.870 --> 10:19.490
It's not Elanor,

140
10:19.520 --> 10:21.980
it's actually spelled a little bit differently.

141
10:21.980 --> 10:27.980
So what I can do on the right-hand pane here is I can right-click on that piece of data that I want

142
10:27.980 --> 10:33.860
to change, and then click Set Value, and then actually change the data here.

143
10:34.850 --> 10:41.300
Now that I've got that spelled out correctly, if I type names and hit Enter, you can see it's now been

144
10:41.300 --> 10:41.810
corrected.

145
10:41.830 --> 10:44.560
And this is my new list of names.

146
10:44.800 --> 10:50.110
So using this list of names, I'm going to try and create a new list of names,

147
10:50.110 --> 10:52.450
but I only want the short names.

148
10:52.450 --> 10:58.360
I only want the names which is made up of four letters or less, like Alex or Beth.

149
10:58.420 --> 11:05.080
The way I would do this is I would create a new list, which is called short_names, and then I would

150
11:05.080 --> 11:06.630
use my list comprehension.

151
11:06.630 --> 11:16.680
And remember in this case the keywords are, "new_item for item in list and then if and a test".

152
11:17.400 --> 11:19.200
So let's fill in the easy parts

153
11:19.200 --> 11:19.770
first.

154
11:19.770 --> 11:22.470
The list is going to be our list of names.

155
11:22.470 --> 11:29.400
The item I can call it anything I want, I'll just call it name in terms of the singular form of names.

156
11:29.400 --> 11:35.750
And then once I loop through each of the names inside my list of names, what is the new item going

157
11:35.750 --> 11:35.990
to be?

158
11:36.020 --> 11:38.030
Well, it's actually going to be unmodified,

159
11:38.030 --> 11:40.370
it's just going to be that same name,

160
11:40.370 --> 11:45.140
but in this case I'm only going to add that name to the list

161
11:45.140 --> 11:47.180
if it passes this test.

162
11:47.180 --> 11:53.060
And this test is going to look at the length of the name which I created here,

163
11:53.060 --> 11:58.910
so if I named this n, then I would be testing it against n and I would be adding n as well,

164
11:58.910 --> 12:01.010
but because I've called it name,

165
12:01.010 --> 12:04.490
so I'm going to use that name right here in the condition.

166
12:04.490 --> 12:13.400
So if the length of the name is less than 5, so it has 4 or less characters, then I'm going to

167
12:13.400 --> 12:15.260
add that name to my new_list.

168
12:15.950 --> 12:22.840
Now when I hit Enter you can see that I've got this new list of short_names and it only contains the

169
12:22.840 --> 12:25.180
names, Alex, Beth, and Dave.

170
12:25.330 --> 12:31.840
So this list comprehension is now a little bit more complex because the first thing it does is it

171
12:31.840 --> 12:35.860
goes through each of the names inside this list of names,

172
12:35.860 --> 12:39.280
it checks each of those names for its length,

173
12:39.280 --> 12:44.670
and if the length is 5, then it adds the name to this new list.

174
12:45.870 --> 12:47.790
Now here's a challenge for you.

175
12:47.820 --> 12:55.590
I want you to take all of the names from this list of names, which are made up of 5 letters or more,

176
12:55.590 --> 12:58.710
so basically, Caroline, Eleanor and Freddy,

177
12:58.830 --> 13:04.380
and I want you to turn each of these names to the uppercase version.

178
13:04.620 --> 13:13.610
What you should end up with is a list where you have Caroline in all caps, Eleanor in all caps, and

179
13:13.610 --> 13:15.440
Freddie in all caps.

180
13:15.440 --> 13:17.390
But of course, you're not going to just type this out,

181
13:17.390 --> 13:19.580
you're going to use list comprehension.

182
13:19.790 --> 13:23.030
Pause the video and see if you can complete this challenge.

183
13:24.830 --> 13:30.320
All right I'm going to call this new list long_names because I haven't eaten for a while and I lack

184
13:30.320 --> 13:31.430
imagination.

185
13:31.610 --> 13:37.540
Now in order to create this list of long_names, I'm going to use that new keyword pattern that we learned,

186
13:37.540 --> 13:43.060
which is, "new_item for item in list if test".

187
13:43.330 --> 13:46.510
The list is again going to be our list of names.

188
13:46.510 --> 13:51.430
Each of the items we can call it anything we want, but I'm going to call it name.

189
13:51.430 --> 13:55.840
And if you find that this is too similar to this, you can call it anything you want,

190
13:55.870 --> 13:56.440
nom,

191
13:56.440 --> 14:01.590
you can use French, you can just write n or you can do anything you want.

192
14:02.550 --> 14:09.030
This name that we're looping through is going to be tested in order to see if we're actually going to

193
14:09.030 --> 14:10.290
create a new_item.

194
14:10.620 --> 14:17.430
The test in this case, is we're checking to see if the length of the name is now greater than 5,

195
14:18.720 --> 14:25.850
and if this name that we're currently looping through passes this test, then we get to specify what

196
14:25.850 --> 14:27.590
the new_item should be.

197
14:27.590 --> 14:33.320
So the new_item is going to be that particular name that passed the test, but then it's going to be

198
14:33.320 --> 14:34.100
uppercased.

199
14:34.100 --> 14:40.220
So we're going to call name.upper() in order to create the new_item that's going to be added to the

200
14:40.220 --> 14:40.820
list.

201
14:40.820 --> 14:45.980
So now once I hit Enter, you can see the new list that I've created.

202
14:45.980 --> 14:51.200
Long names has Caroline, Eleanor, and Freddie all in uppercase.

203
14:51.470 --> 14:54.770
Essentially, we looped through each of the names in the list,

204
14:54.770 --> 14:58.550
we took each of those names and checked that they're longer than 5,

205
14:58.550 --> 15:00.860
if they were in fact longer than 5,

206
15:00.860 --> 15:05.990
then we looked at this part of the code to see what we should do with each of those names, and in our

207
15:05.990 --> 15:08.120
case, we've turned it into uppercase.

208
15:08.300 --> 15:12.060
So there's quite a lot of theory covered in today's lesson.

209
15:12.090 --> 15:17.220
Now, in order to be able to fully understand what's going on, you can't just watch me talk about it,

210
15:17.220 --> 15:18.840
you have to practice.

211
15:18.870 --> 15:25.050
So in the coming lessons, I've got a whole bunch of exercises for you to practice using and creating

212
15:25.050 --> 15:26.850
list comprehensions for yourself.

213
15:26.850 --> 15:31.410
And that way, once you've done the drills, you've done the code press-ups, then you'll be able to

214
15:31.410 --> 15:35.550
show off your list comprehension muscles. For all of that and more,

215
15:35.550 --> 15:36.900
I'll see you on the next lesson.