WEBVTT

1
00:00:01.570 --> 00:00:03.080
<v ->Hi guys and welcome back.</v>

2
00:00:03.080 --> 00:00:04.610
In this video, we're going to talk about

3
00:00:04.610 --> 00:00:07.250
list comprehensions in Python.

4
00:00:07.250 --> 00:00:09.940
A list comprehension is a great feature of Python

5
00:00:09.940 --> 00:00:13.260
and allows us to create new lists on the fly

6
00:00:13.260 --> 00:00:14.950
from existing lists.

7
00:00:14.950 --> 00:00:17.840
In addition, it's very succinct and very powerful.

8
00:00:17.840 --> 00:00:19.100
Let's get started.

9
00:00:19.100 --> 00:00:21.450
Here we've got a list of numbers,

10
00:00:21.450 --> 00:00:24.090
and I want to end up with these numbers,

11
00:00:24.090 --> 00:00:25.920
but multiplied by two,

12
00:00:25.920 --> 00:00:29.560
so I'll say something like doubled equal empty list.

13
00:00:29.560 --> 00:00:32.290
How do we put each of these numbers,

14
00:00:32.290 --> 00:00:35.130
multiplied by two, into this new list?

15
00:00:35.130 --> 00:00:37.000
Traditionally and in most languages,

16
00:00:37.000 --> 00:00:40.740
you would end up doing something like for num in numbers,

17
00:00:40.740 --> 00:00:45.507
and then you'd do doubled.append num times two.

18
00:00:46.980 --> 00:00:48.640
This is quite a long piece of code

19
00:00:48.640 --> 00:00:51.820
just for creating a new list of doubled numbers,

20
00:00:51.820 --> 00:00:53.900
but this is how you do it normally.

21
00:00:53.900 --> 00:00:56.410
You would iterate over the numbers list,

22
00:00:56.410 --> 00:00:59.220
create a new variable for each of the numbers in it,

23
00:00:59.220 --> 00:01:01.450
and then run through it three times,

24
00:01:01.450 --> 00:01:03.620
each time putting the appropriate number,

25
00:01:03.620 --> 00:01:06.340
multiplied by two, into your new list.

26
00:01:06.340 --> 00:01:08.970
Python has a much more succinct way of doing this

27
00:01:08.970 --> 00:01:10.950
which is with a list comprehension.

28
00:01:10.950 --> 00:01:12.190
In a list comprehension,

29
00:01:12.190 --> 00:01:16.310
the order of these operations, first the iteration,

30
00:01:16.310 --> 00:01:19.810
and then the appending, seems to be reversed,

31
00:01:19.810 --> 00:01:21.570
but it's not really.

32
00:01:21.570 --> 00:01:23.920
So the first thing you do is you would put in

33
00:01:23.920 --> 00:01:26.970
what you want to add to your new list,

34
00:01:26.970 --> 00:01:28.673
which is num times two.

35
00:01:29.640 --> 00:01:32.800
But of course, num doesn't exist in this context.

36
00:01:32.800 --> 00:01:36.250
It only existed here because we've got our for loop.

37
00:01:36.250 --> 00:01:38.883
So the next thing you do is you put your for loop,

38
00:01:40.534 --> 00:01:44.200
and what this does in Python is it puts num times two

39
00:01:44.200 --> 00:01:49.200
into a new list for every num in the numbers list.

40
00:01:49.710 --> 00:01:52.970
So essentially you're iterating over the numbers list,

41
00:01:52.970 --> 00:01:56.216
and you're putting the variable multiplied by two

42
00:01:56.216 --> 00:02:00.600
every time that the loop runs into your new list.

43
00:02:00.600 --> 00:02:03.850
You end up with the doubled numbers.

44
00:02:03.850 --> 00:02:06.790
Because list comprehension are usually in a single line,

45
00:02:06.790 --> 00:02:09.740
you do want to keep them short if you can,

46
00:02:09.740 --> 00:02:12.210
so instead of num times two for example in this case,

47
00:02:12.210 --> 00:02:15.640
you could do x times two, and for x numbers.

48
00:02:15.640 --> 00:02:17.780
That just helps you keep it a little bit shorter

49
00:02:17.780 --> 00:02:19.280
and a little bit more readable.

50
00:02:19.280 --> 00:02:20.870
After all, the most important thing here

51
00:02:20.870 --> 00:02:25.050
is that you're putting x times two into your new list

52
00:02:25.050 --> 00:02:27.510
while you iterate over the previous list.

53
00:02:27.510 --> 00:02:31.000
So again to recap, how you build a list comprehension

54
00:02:31.000 --> 00:02:33.900
is what you wanna put into your new list

55
00:02:33.900 --> 00:02:38.583
for the variable that you're using in numbers.

56
00:02:40.150 --> 00:02:42.740
Now let's say you've got a friend's list,

57
00:02:42.740 --> 00:02:44.730
and you wanna create a new list

58
00:02:44.730 --> 00:02:47.370
which contains the names of those friends

59
00:02:47.370 --> 00:02:49.500
that start with an s.

60
00:02:49.500 --> 00:02:51.820
So Sam, Samantha, and Saurabh.

61
00:02:51.820 --> 00:02:54.140
But it seems that a list comprehension

62
00:02:54.140 --> 00:02:56.070
may not be the tool for this job.

63
00:02:56.070 --> 00:03:00.030
After all, you do need to only get some of these out.

64
00:03:00.030 --> 00:03:01.600
Let's start with a for loop,

65
00:03:01.600 --> 00:03:03.580
and then I'll show you how the list comprehension

66
00:03:03.580 --> 00:03:04.920
can deal with it.

67
00:03:04.920 --> 00:03:08.160
So you would do for friend in friends.

68
00:03:08.160 --> 00:03:10.660
Then you would add an if statement to check

69
00:03:10.660 --> 00:03:14.030
that the friend name in question starts with an s.

70
00:03:14.030 --> 00:03:18.293
You could say if friend.startswith capital s,

71
00:03:19.340 --> 00:03:24.010
then you're gonna do starts_s.append friend,

72
00:03:24.010 --> 00:03:28.660
and then at the end, we're gonna print starts_s like that.

73
00:03:28.660 --> 00:03:30.900
Let me run this and show you what happens.

74
00:03:30.900 --> 00:03:33.870
Down here, you have Sam, Samantha, and Saurabh,

75
00:03:33.870 --> 00:03:35.370
and I think I'm not pronouncing that right,

76
00:03:35.370 --> 00:03:38.120
so my apologies if that's the case.

77
00:03:38.120 --> 00:03:40.580
So what we've got is a for loop,

78
00:03:40.580 --> 00:03:44.310
and then an if statement, and then an append.

79
00:03:44.310 --> 00:03:47.310
This structure here can also be reproduced

80
00:03:47.310 --> 00:03:50.780
with a list comprehension although it does start to get

81
00:03:50.780 --> 00:03:52.363
a little bit more confusing.

82
00:03:53.500 --> 00:03:57.140
Again, the process is more or less the same.

83
00:03:57.140 --> 00:04:00.480
First of all, you put what you wanna add to your new list,

84
00:04:00.480 --> 00:04:01.873
so that's your friend name.

85
00:04:02.780 --> 00:04:05.480
Then you put your iteration or your for loop,

86
00:04:05.480 --> 00:04:08.020
for friend in friends.

87
00:04:08.020 --> 00:04:10.760
And now if you wanna add an if statement,

88
00:04:10.760 --> 00:04:12.040
you do it at the end,

89
00:04:12.040 --> 00:04:13.970
so you're gonna add your friend

90
00:04:13.970 --> 00:04:18.970
for the friend in your list if the friend starts with s,

91
00:04:21.830 --> 00:04:23.340
and that does exactly the same thing,

92
00:04:23.340 --> 00:04:26.020
and you no longer need that loop there,

93
00:04:26.020 --> 00:04:27.570
so you can save that and run it again,

94
00:04:27.570 --> 00:04:30.230
and you see that the same thing comes out.

95
00:04:30.230 --> 00:04:33.870
So this is how you add a conditional, an if statement,

96
00:04:33.870 --> 00:04:36.023
into a list comprehension.

97
00:04:37.170 --> 00:04:38.650
Very important to nice that

98
00:04:38.650 --> 00:04:40.770
when you use a list comprehension,

99
00:04:40.770 --> 00:04:43.430
a new list is created,

100
00:04:43.430 --> 00:04:47.930
so even though these two lists may be the same

101
00:04:47.930 --> 00:04:49.483
if we take these names out,

102
00:04:52.350 --> 00:04:53.860
so now these two lists will be the same

103
00:04:53.860 --> 00:04:55.210
if we print them both out,

104
00:04:55.210 --> 00:04:57.210
you'll see that they are both identical.

105
00:04:58.490 --> 00:05:01.500
When you print friends is starts_s,

106
00:05:01.500 --> 00:05:03.713
you'll see that false comes out.

107
00:05:05.430 --> 00:05:07.220
That's because these two lists,

108
00:05:07.220 --> 00:05:09.470
although they have the same content,

109
00:05:09.470 --> 00:05:11.370
are not the same list.

110
00:05:11.370 --> 00:05:13.880
They are two different lists, two different places

111
00:05:13.880 --> 00:05:17.803
in your computer's memory with the same values.

112
00:05:18.840 --> 00:05:22.460
For context, friends zero is starts_s a zero,

113
00:05:22.460 --> 00:05:27.460
so Sam is Sam will give you true, see that?

114
00:05:27.640 --> 00:05:30.380
But the lists themselves are not the same,

115
00:05:30.380 --> 00:05:31.960
so the elements in them are the same,

116
00:05:31.960 --> 00:05:34.270
but the lists themselves are not the same,

117
00:05:34.270 --> 00:05:38.860
and by using the is keyword, then you can check that,

118
00:05:38.860 --> 00:05:41.840
but there's also something else you can do,

119
00:05:41.840 --> 00:05:44.870
which is check the IDs of these lists,

120
00:05:44.870 --> 00:05:47.833
so you can do something like friends,

121
00:05:49.160 --> 00:05:51.620
then print out the ID of friends,

122
00:05:51.620 --> 00:05:56.620
and then starts_s and print the ID of starts-s,

123
00:05:57.310 --> 00:05:58.460
and what I've done here by the way

124
00:05:58.460 --> 00:06:01.470
is I have passed multiple different things

125
00:06:01.470 --> 00:06:03.937
to the print function, and you can do that in print,

126
00:06:03.937 --> 00:06:05.610
and it just separates them with a space,

127
00:06:05.610 --> 00:06:08.780
so it's gonna separate these different parts with a space,

128
00:06:08.780 --> 00:06:10.030
the different arguments there.

129
00:06:10.030 --> 00:06:15.030
So by running this, you get that friends has an ID

130
00:06:16.780 --> 00:06:21.530
of this, ends in 2096, and starts_s has an ID

131
00:06:21.530 --> 00:06:24.120
that ends in 5280.

132
00:06:24.120 --> 00:06:27.560
So these two have different IDs, and in Cpython,

133
00:06:27.560 --> 00:06:29.040
which is what most of you will be using,

134
00:06:29.040 --> 00:06:30.330
certainly what I'm using,

135
00:06:30.330 --> 00:06:32.997
the ID is related to the memory address

136
00:06:32.997 --> 00:06:35.360
in which the list is stored,

137
00:06:35.360 --> 00:06:37.330
so you can see that these two things

138
00:06:37.330 --> 00:06:38.710
are in different places in memory.

139
00:06:38.710 --> 00:06:40.960
They're different objects entirely.

140
00:06:40.960 --> 00:06:43.650
They just happen to have the same contents.

141
00:06:43.650 --> 00:06:46.510
So this is something that will be very important

142
00:06:46.510 --> 00:06:48.340
as we proceed with the course,

143
00:06:48.340 --> 00:06:51.060
so this is why I wanted to show it to you at this point.

144
00:06:51.060 --> 00:06:54.140
Creating new lists gives you a different thing

145
00:06:54.140 --> 00:06:55.680
entirely in Python.

146
00:06:55.680 --> 00:06:57.560
It's not the same as the thing before

147
00:06:57.560 --> 00:06:59.410
even if it has the same contents,

148
00:06:59.410 --> 00:07:00.350
but as I mentioned earlier,

149
00:07:00.350 --> 00:07:03.890
if you do want them to be the same exact list,

150
00:07:03.890 --> 00:07:05.760
then you can do this.

151
00:07:05.760 --> 00:07:08.610
Starts_s equal friends, and now when you press play,

152
00:07:08.610 --> 00:07:09.730
you'll see that you get true,

153
00:07:09.730 --> 00:07:11.760
and these IDs are identical.

154
00:07:11.760 --> 00:07:15.620
So that is how you make two lists the same thing.

155
00:07:15.620 --> 00:07:18.500
Otherwise, you do end up with two different lists.

156
00:07:18.500 --> 00:07:19.730
All right, that's enough of that.

157
00:07:19.730 --> 00:07:21.020
Thank you for joining me in this video.

158
00:07:21.020 --> 00:07:22.140
I hope it's been useful,

159
00:07:22.140 --> 00:07:23.790
and I'll see you in the next one.

