1
00:00:00,480 --> 00:00:05,480
The big topic that we're going to explore today is a concept known as a list

2
00:00:05,970 --> 00:00:08,850
comprehension. And as I mentioned before,

3
00:00:08,850 --> 00:00:11,880
this is something that's really unique to the Python language.

4
00:00:12,210 --> 00:00:13,980
In many other programming languages,

5
00:00:14,010 --> 00:00:17,490
you don't actually have access to something like this.

6
00:00:17,520 --> 00:00:19,500
There's nothing that really compares to it.

7
00:00:19,830 --> 00:00:22,350
And it's something that Python developers really,

8
00:00:22,350 --> 00:00:26,820
really love because it cuts down on the amount of typing and it just makes the

9
00:00:26,820 --> 00:00:30,840
code a lot shorter, and in most cases, a lot easier to read.

10
00:00:31,520 --> 00:00:36,410
So what exactly is a list comprehension? Well, it's a case where you 

11
00:00:36,410 --> 00:00:36,770
create

12
00:00:36,770 --> 00:00:41,770
a new list from a previous list. So far we've been doing that using for loops.

13
00:00:42,200 --> 00:00:45,830
So for example, if I have a list of numbers, 1, 2, 3,

14
00:00:46,280 --> 00:00:51,280
and I want to create a new list where each number is increased by one,

15
00:00:52,370 --> 00:00:52,520
well

16
00:00:52,520 --> 00:00:57,260
then I would use a for loop creating a new empty list and then looping through

17
00:00:57,350 --> 00:01:01,670
each of the numbers in that list in order to add one to

18
00:01:01,700 --> 00:01:02,810
each of those numbers.

19
00:01:03,170 --> 00:01:08,170
And then I would append each of those increased numbers to my new list.

20
00:01:08,870 --> 00:01:13,100
And I would end up with a new list that consists of three items,

21
00:01:13,370 --> 00:01:17,810
and it will be 2, 3, and 4. Now using list comprehension

22
00:01:17,900 --> 00:01:22,520
we can take these four lines of code and turn it into one.

23
00:01:23,060 --> 00:01:27,740
And the way that we create a list comprehension looks something like this.

24
00:01:28,340 --> 00:01:32,450
I like to use the keyword method where you type out the list comprehension

25
00:01:32,450 --> 00:01:33,283
keywords,

26
00:01:33,530 --> 00:01:38,390
and then replace each of the words with the actual item in your code.

27
00:01:39,170 --> 00:01:43,520
The way that we would create any list comprehension is we will create the name

28
00:01:43,550 --> 00:01:48,550
of the new list and instead of creating a empty list and then having to append

29
00:01:49,040 --> 00:01:53,420
to it or populate it, we actually going to create it straight in the same line.

30
00:01:53,840 --> 00:01:58,220
So we open up a set of square brackets to denote that we're creating a new list,

31
00:01:58,640 --> 00:02:03,170
and then we have this new _item for item in list

32
00:02:03,320 --> 00:02:06,020
keyword pattern. Here's how it would work.

33
00:02:06,050 --> 00:02:08,419
Let's take that same thing that we did before

34
00:02:08,660 --> 00:02:10,520
where we took you a list of numbers

35
00:02:10,580 --> 00:02:14,240
and we created a new list where each number is increased by one,

36
00:02:14,480 --> 00:02:17,300
and let's see how we would do it using list comprehension.

37
00:02:18,590 --> 00:02:22,040
So the first keyword I'm going to replace is the list.

38
00:02:22,280 --> 00:02:26,210
So this is the list that we're going to iterate through. And in our case,

39
00:02:26,240 --> 00:02:30,110
it's this list of numbers. So let's replace that list

40
00:02:30,110 --> 00:02:33,440
keyword with the actual list, which is numbers.

41
00:02:34,010 --> 00:02:38,210
The next thing that is easy to understand is each item in the list.

42
00:02:38,210 --> 00:02:41,600
So we can call this anything we want. In the for loop here,

43
00:02:41,600 --> 00:02:46,190
we called it n, so why don't we go ahead and call it n again?

44
00:02:47,870 --> 00:02:52,700
This n is going to represent each of the numbers in that list of numbers

45
00:02:52,760 --> 00:02:54,740
just as it does in this for-loop.

46
00:02:55,460 --> 00:02:59,590
And the final keyword that we haven't yet replaced is the new_item.

47
00:03:00,010 --> 00:03:04,270
So what do we want each of the new items to be in this new list?

48
00:03:04,690 --> 00:03:05,200
Well,

49
00:03:05,200 --> 00:03:10,200
each of the new items is basically calculated by taking n and then adding one

50
00:03:10,750 --> 00:03:11,500
to it.

51
00:03:11,500 --> 00:03:16,500
So this line is the expression or the code that we need to execute in order to

52
00:03:17,560 --> 00:03:21,910
get a new_item. So this becomes our final list

53
00:03:21,910 --> 00:03:25,060
comprehension. We have our lists of numbers

54
00:03:25,150 --> 00:03:29,620
and then we create a new list where we loop through each of the numbers, n,

55
00:03:29,950 --> 00:03:31,750
inside this list of numbers

56
00:03:32,080 --> 00:03:37,080
and then for each of the n, we add one to it in order to create the final list.

57
00:03:37,570 --> 00:03:42,310
So when you look at a list comprehension just like this as in the real code,

58
00:03:42,610 --> 00:03:45,700
it can be a little bit confusing to see how would you read it,

59
00:03:45,730 --> 00:03:47,260
which order does it happen.

60
00:03:47,620 --> 00:03:50,920
But when you're using this keyword method that I often use

61
00:03:50,920 --> 00:03:55,240
where you just type out what you need to have inside a list comprehension,

62
00:03:55,240 --> 00:03:58,390
you need a name for the new list, you need a set of square brackets

63
00:03:58,660 --> 00:04:02,680
and then inside the square brackets, you have new_item for item in list.

64
00:04:03,010 --> 00:04:07,390
Then you go through it replacing each of the keywords, list item

65
00:04:07,390 --> 00:04:12,340
and new_item. So I want you to have a go at actually writing out this code.

66
00:04:12,640 --> 00:04:15,310
Go ahead and create a new PyCharm project,

67
00:04:15,670 --> 00:04:18,220
and I want you to go into the Python console.

68
00:04:18,700 --> 00:04:21,940
We're not actually going to be writing code inside our main.py.

69
00:04:22,300 --> 00:04:26,770
And the reason is because the console allows us to write code line by line

70
00:04:27,070 --> 00:04:31,150
and then we can view our variables really easily on the right-hand side pane

71
00:04:31,150 --> 00:04:35,650
here. For example, if I have my list of numbers, 1, 2, 3,

72
00:04:36,130 --> 00:04:39,310
and then I have to write out my list comprehension

73
00:04:39,340 --> 00:04:42,610
where we create our new list which I'll call new_numbers,

74
00:04:43,150 --> 00:04:46,510
and then we have to create our list comprehension.

75
00:04:46,600 --> 00:04:51,600
So the way to remember it is new_item for item in list.

76
00:04:52,660 --> 00:04:57,660
So now go ahead and write this out and see if you can remember how we actually

77
00:04:58,150 --> 00:05:02,830
structure our list comprehension and see if you can get it to work within your

78
00:05:02,830 --> 00:05:06,340
PyCharm console. So pause the video, give that a go.

79
00:05:08,170 --> 00:05:11,500
All right. So our list is of course, this list of numbers.

80
00:05:11,530 --> 00:05:13,180
So let's replace that first.

81
00:05:13,660 --> 00:05:16,930
And then for each of the items in that list of numbers, well,

82
00:05:16,930 --> 00:05:21,070
we can call it anything we want. So I'm just going to call it n or num,

83
00:05:21,310 --> 00:05:25,300
it doesn't matter. But as long as you're consistent in using it

84
00:05:25,540 --> 00:05:26,950
when you create the new item

85
00:05:27,340 --> 00:05:30,670
because each new item is going to be the n,

86
00:05:30,730 --> 00:05:34,210
which is each number in the list, +1.

87
00:05:34,750 --> 00:05:36,400
So now when I hit enter,

88
00:05:36,460 --> 00:05:41,410
you can see I've got this new list called new_numbers created from the previous

89
00:05:41,410 --> 00:05:45,550
list, numbers, and this now has the same number of items

90
00:05:45,580 --> 00:05:48,160
but each item is now increased by one.

91
00:05:49,480 --> 00:05:52,960
Now it's important to remember that when we say list comprehension,

92
00:05:53,200 --> 00:05:56,680
it doesn't strictly mean that you can only work with list.

93
00:05:56,930 --> 00:06:00,710
You can work with other sequences like strings, for example.

94
00:06:00,950 --> 00:06:05,630
So here I've got a variable called name, which is just a string. It's the word

95
00:06:05,690 --> 00:06:06,523
Angela.

96
00:06:06,770 --> 00:06:11,770
And I'm creating a new list by using a list comprehension that has this kind of

97
00:06:13,520 --> 00:06:14,510
code. Now,

98
00:06:14,510 --> 00:06:19,510
I want you to pause for a second and think about what would be in this new list,

99
00:06:19,610 --> 00:06:24,050
what would it look like? And then I want you to try it out inside PyCharm.

100
00:06:24,350 --> 00:06:28,460
So pause the video now. All right.

101
00:06:28,460 --> 00:06:32,900
So name equals Angela, this is the variable that we're working with.

102
00:06:33,500 --> 00:06:35,630
And then I'm going to create a new list

103
00:06:35,720 --> 00:06:39,290
which I'll call letters_list.

104
00:06:40,550 --> 00:06:44,600
This new list is going to be created using my list comprehension.

105
00:06:44,990 --> 00:06:49,990
So it's going to be letter for letter in name. And what this does is it takes

106
00:06:53,510 --> 00:06:56,330
this sequence, this string,

107
00:06:56,780 --> 00:06:59,750
it goes through each of the letters in that string

108
00:07:00,230 --> 00:07:04,640
and then it adds each of the letters into this new list.

109
00:07:05,060 --> 00:07:08,210
So when I hit enter, you can see, this is what letters_list

110
00:07:08,210 --> 00:07:13,010
looks like. It's just split off my string into individual letters

111
00:07:13,280 --> 00:07:15,950
and it's added it into a brand new list.

112
00:07:17,390 --> 00:07:20,390
As I mentioned, these things in Python

113
00:07:20,390 --> 00:07:25,390
like a list or a string or a range or tuple, they're called sequences because

114
00:07:27,800 --> 00:07:32,630
they have a specific order. And when you perform a list comprehension,

115
00:07:32,660 --> 00:07:37,280
it's going to take that sequence and it's going to go through it in order either

116
00:07:37,280 --> 00:07:41,870
be it the letters in the string or the items in a list.

117
00:07:42,230 --> 00:07:46,280
And then it's going to take each of those items in that correct order

118
00:07:46,490 --> 00:07:50,450
and then do something with it, either add one to it, or in this case,

119
00:07:50,450 --> 00:07:54,650
do nothing and just add it to a new list. Here's a challenge for you.

120
00:07:55,010 --> 00:07:56,720
Can you take the range,

121
00:07:56,750 --> 00:08:01,670
which is also a sequence that we can iterate through, and then create a range

122
00:08:01,670 --> 00:08:03,440
between 1 and 5?

123
00:08:03,800 --> 00:08:07,940
And remember that the way that the range works is it's going to take 1 and

124
00:08:07,940 --> 00:08:12,590
then 2, and then 3 and then 4, but it's not going to go up to 5.

125
00:08:13,070 --> 00:08:17,630
I want you to loop through this range and then create a list where each of the

126
00:08:17,630 --> 00:08:20,630
numbers in the range is doubled.

127
00:08:21,170 --> 00:08:25,760
And the final list should look like this. It will be 2, 4, 6,

128
00:08:25,820 --> 00:08:29,780
and 8. Pause the video and see if you can complete this challenge.

129
00:08:32,450 --> 00:08:34,730
All right. So let's create our new lists

130
00:08:34,760 --> 00:08:37,039
which I'm just going to call range_list.

131
00:08:37,429 --> 00:08:40,340
And we're going to use that keyword pattern to remind ourselves.

132
00:08:40,610 --> 00:08:44,570
So it was going to be new_item for item in list.

133
00:08:45,110 --> 00:08:48,380
And this list, in this case, is actually not going to be a list,

134
00:08:48,380 --> 00:08:50,810
it is going to be a range between 1 and 5.

135
00:08:51,320 --> 00:08:55,220
And then each of the items, we get to give it a name, which I'll just call num.

136
00:08:55,710 --> 00:09:00,510
And then in this new list, what is each new item going to be? Well,

137
00:09:00,510 --> 00:09:05,040
it's going to be num multiplied by 2. So now when I hit enter,

138
00:09:05,070 --> 00:09:08,640
I get my range_list, which is 2, 4, 6, 8.

139
00:09:09,030 --> 00:09:11,760
So its loop through each of the numbers in the range

140
00:09:11,940 --> 00:09:14,340
and its multiplied each of those numbers by 2,

141
00:09:14,670 --> 00:09:18,750
and then its created a new list using each of those numbers.

142
00:09:19,920 --> 00:09:24,420
Now the final thing I want to show you regarding list comprehensions is that

143
00:09:24,420 --> 00:09:28,590
there's also such a thing as a conditional list comprehension.

144
00:09:29,010 --> 00:09:31,860
This takes our keywords a little bit further.

145
00:09:31,880 --> 00:09:35,030
Whereas previously we stopped right here,

146
00:09:35,900 --> 00:09:40,730
new_item for item in list. We can also tag on two more keywords;

147
00:09:40,850 --> 00:09:42,410
if and a test.

148
00:09:43,070 --> 00:09:48,070
What this is going to allow us to do is to only add this new item and only to

149
00:09:48,800 --> 00:09:53,660
perform this code if the test actually passes.

150
00:09:54,920 --> 00:09:59,780
Here's an example. I've got a bunch of names here; Alex, Beth, Caroline,

151
00:09:59,780 --> 00:10:02,120
Dave, Eleanor, and Freddy. Now,

152
00:10:02,120 --> 00:10:06,980
one of the neat things that you can actually do with the Python console is not

153
00:10:06,980 --> 00:10:10,160
only can you view what all of the variables are equal to,

154
00:10:10,370 --> 00:10:12,530
but you can also edit them if you have an issue.

155
00:10:13,010 --> 00:10:17,300
The sharp-eyed amongst you will know that I've actually made a typo in this

156
00:10:17,300 --> 00:10:21,800
name, it's not Elanor, its actually spelled a little bit differently.

157
00:10:22,280 --> 00:10:27,020
So what I can do on the right-hand pane here is I can right-click on that piece

158
00:10:27,020 --> 00:10:30,860
of data that I want to change and then click set value,

159
00:10:31,280 --> 00:10:33,740
and then actually change the data here.

160
00:10:35,120 --> 00:10:39,530
Now that I've got that spelled out correctly, if I type names and hit enter,

161
00:10:39,770 --> 00:10:44,360
you can see it's now been corrected and this is my new list of names.

162
00:10:45,170 --> 00:10:46,820
So using this list of names,

163
00:10:47,180 --> 00:10:50,030
I'm going to try and create a new list of names,

164
00:10:50,360 --> 00:10:52,460
but I only want the short names.

165
00:10:52,550 --> 00:10:57,550
I only want the names which is made up of four letters or less, like Alex or

166
00:10:57,620 --> 00:10:58,453
Beth.

167
00:10:58,700 --> 00:11:02,750
The way I would do this is I would create a new list,

168
00:11:02,780 --> 00:11:06,530
which is called short_names. And then I would use my list comprehension.

169
00:11:06,800 --> 00:11:11,720
And remember, in this case, the keywords are new_item

170
00:11:11,810 --> 00:11:16,550
for item in list. And then if and a test.

171
00:11:17,540 --> 00:11:19,670
So let's fill in the easy parts first.

172
00:11:20,060 --> 00:11:24,290
The list is going to be our list of names, the item I can call it

173
00:11:24,290 --> 00:11:29,210
anything I want. I'll just call it name in terms of the singular form of names.

174
00:11:29,720 --> 00:11:33,770
And then once I loop through each of the names inside my list of names,

175
00:11:34,100 --> 00:11:38,090
what is the new item going to be? Well, it's actually going to be unmodified.

176
00:11:38,090 --> 00:11:42,350
It's just going to be that same name. But in this case,

177
00:11:42,380 --> 00:11:46,940
I'm only going to add that name to the list if it passes this test.

178
00:11:47,540 --> 00:11:51,140
And this test is going to look at the length of the name

179
00:11:51,260 --> 00:11:53,030
which I created here,

180
00:11:53,290 --> 00:11:57,940
so if I named this n then I would be testing it against n and it would be adding

181
00:11:57,940 --> 00:12:01,060
n as well. But because I've called it name

182
00:12:01,060 --> 00:12:04,300
so I'm going to use that name right here in the condition.

183
00:12:04,810 --> 00:12:07,900
So if the length of the name is less than five,

184
00:12:08,230 --> 00:12:11,620
so it has four or less characters,

185
00:12:12,370 --> 00:12:17,350
then I'm going to add that name to my new list. Now, when I hit enter,

186
00:12:17,710 --> 00:12:22,710
you can see that I've got this new list of short names and it only contains the

187
00:12:22,870 --> 00:12:24,970
names, Alex, Beth and Dave.

188
00:12:25,630 --> 00:12:30,580
So this list comprehension is now a little bit more complex because the first

189
00:12:30,580 --> 00:12:35,580
thing it does is it goes through each of the names inside this list of names,

190
00:12:36,100 --> 00:12:41,080
it checks each of those names for its length. And if the length is five,

191
00:12:41,350 --> 00:12:44,530
then it adds the name to this new list.

192
00:12:46,180 --> 00:12:47,620
Now here's a challenge for you.

193
00:12:48,130 --> 00:12:52,120
I want you to take all of the names from this list of names

194
00:12:52,570 --> 00:12:57,340
which are made up of five letters or more, so basically Caroline,

195
00:12:57,370 --> 00:12:58,720
Eleanor and Freddie,

196
00:12:59,050 --> 00:13:04,050
and I want you to turn each of these names to the uppercase version.

197
00:13:04,720 --> 00:13:09,720
What you should end up with is a list where you have CAROLINE in all caps,

198
00:13:10,960 --> 00:13:15,430
ELEANOR in all caps and FREDDIE in all caps.

199
00:13:15,670 --> 00:13:17,470
But of course, you're not going to just type this out,

200
00:13:17,470 --> 00:13:19,420
you're going to use list comprehension.

201
00:13:19,930 --> 00:13:22,900
Pause the video and see if you can complete this challenge.

202
00:13:25,060 --> 00:13:27,790
All right. I'm going to call this new list long_names,

203
00:13:27,820 --> 00:13:32,170
cause I haven't eaten for a while and I lack imagination. Now,

204
00:13:32,260 --> 00:13:34,450
in order to create this list of long names,

205
00:13:34,510 --> 00:13:37,450
I'm going to use that new keyword pattern that we learned,

206
00:13:37,780 --> 00:13:41,530
which is new item for item in list

207
00:13:41,800 --> 00:13:46,300
if test. The list is, again, going to be our list of names.

208
00:13:46,780 --> 00:13:49,540
Each of the items we can call it anything we want,

209
00:13:49,690 --> 00:13:54,430
but I'm going to call it name. And if you find that this is too similar to this,

210
00:13:54,700 --> 00:13:55,870
you can call anything you want.

211
00:13:55,900 --> 00:14:00,900
Nom, you can use French or you can just write n or you can do anything you want.

212
00:14:02,830 --> 00:14:07,830
This name that we're looping through is going to be tested in order to see if

213
00:14:08,350 --> 00:14:10,120
we're actually going to create a new item.

214
00:14:10,600 --> 00:14:15,600
So the test, in this case, is we're checking to see if the length of the name is

215
00:14:15,760 --> 00:14:17,290
now greater than five.

216
00:14:19,090 --> 00:14:23,890
And if this name that we're currently looping through passes this test,

217
00:14:24,160 --> 00:14:27,340
then we get to specify what the new item should be.

218
00:14:27,940 --> 00:14:31,870
So the new item is going to be that particular name that passed the test,

219
00:14:32,260 --> 00:14:34,060
but then it's going to be uppercased,

220
00:14:34,360 --> 00:14:38,950
so we're going to call name.upper in order to create the new item

221
00:14:38,980 --> 00:14:42,850
that's going to be added to the list. So now once I hit enter,

222
00:14:43,030 --> 00:14:47,770
you can see the new list that I've created, long_names, has Caroline,

223
00:14:47,800 --> 00:14:50,920
Eleanor and Freddy, all in uppercase.

224
00:14:51,560 --> 00:14:54,770
Essentially, we looped through each of the names in the list,

225
00:14:55,010 --> 00:14:58,370
we took each of those names and checked that they'ree longer than five.

226
00:14:58,760 --> 00:15:00,860
If they were, in fact, longer than five,

227
00:15:00,920 --> 00:15:04,640
then we looked at this part of the code to see what we should do with each of

228
00:15:04,640 --> 00:15:07,910
those names. And in our case, we've turned it into uppercase.

229
00:15:08,600 --> 00:15:12,020
So there's quite a lot of theory covered in today's lesson.

230
00:15:12,320 --> 00:15:15,260
And if you want to see the code that I've been writing so far,

231
00:15:15,530 --> 00:15:20,420
then you can always head over to the day-26-end Repl.it in order to either

232
00:15:20,420 --> 00:15:25,310
download it or just read through the code that I've been typing. Now,

233
00:15:25,340 --> 00:15:27,800
in order to be able to fully understand what's going on,

234
00:15:28,010 --> 00:15:31,580
you can't just watch me talk about it. You have to practice.

235
00:15:31,850 --> 00:15:33,230
So in the coming lessons,

236
00:15:33,260 --> 00:15:38,090
I've got a whole bunch of exercises for you to practice using and creating list

237
00:15:38,090 --> 00:15:41,480
comprehensions for yourself. And that way, once you've done the drills,

238
00:15:41,750 --> 00:15:45,050
you've done the code press-ups, then you'll be able to show off your list

239
00:15:45,050 --> 00:15:49,490
comprehension muscles. For all of that and more, I'll see you on the next lesson.

