WEBVTT 1 00:00:00.180 --> 00:00:01.980 line:15% Hi guys, and welcome back. 2 00:00:01.980 --> 00:00:03.550 line:15% In this video, we're going to talk 3 00:00:03.550 --> 00:00:06.020 line:15% about list comprehensions. 4 00:00:06.020 --> 00:00:08.242 List comprehensions are a feature of Python 5 00:00:08.242 --> 00:00:11.830 that allows us to create new lists very succinctly 6 00:00:11.830 --> 00:00:14.040 while also being very powerful. 7 00:00:14.040 --> 00:00:15.870 Let's say you've got a list of numbers, 8 00:00:15.870 --> 00:00:17.930 and you want to create a new list 9 00:00:17.930 --> 00:00:20.773 which contains those numbers multiplied by two. 10 00:00:21.690 --> 00:00:24.349 So you'd start with something like numbers equal 11 00:00:24.349 --> 00:00:27.110 and the numbers from zero to four say, 12 00:00:27.110 --> 00:00:30.240 and then you want double numbers to contain 13 00:00:30.240 --> 00:00:33.360 those numbers, but every number multiplied by two. 14 00:00:33.360 --> 00:00:35.820 What you'd have to do without a list comprehension 15 00:00:35.820 --> 00:00:39.870 is you would have a for to go over each number 16 00:00:39.870 --> 00:00:41.430 in the numbers list, 17 00:00:41.430 --> 00:00:45.120 and then you would have to say double numbers dot append 18 00:00:45.120 --> 00:00:48.920 to add a new number to the doubled numbers list, 19 00:00:48.920 --> 00:00:51.863 and you can append number times two. 20 00:00:52.990 --> 00:00:55.320 Then at the end, you can print doubled numbers. 21 00:00:55.320 --> 00:00:56.900 So this would do what you want. 22 00:00:56.900 --> 00:00:58.860 If you run this, you'll see that the output 23 00:00:58.860 --> 00:01:02.300 is zero, two, four, six, eight. 24 00:01:02.300 --> 00:01:04.220 Instead of doing this, 25 00:01:04.220 --> 00:01:06.500 you can use a list comprehension which actually has 26 00:01:06.500 --> 00:01:09.090 similar syntax in many places, 27 00:01:09.090 --> 00:01:10.840 but it's much shorter, 28 00:01:10.840 --> 00:01:12.320 and once you're accustomed to it, 29 00:01:12.320 --> 00:01:14.460 it's also a little bit easier to read. 30 00:01:14.460 --> 00:01:19.120 How you do that is you would say doubled numbers equals, 31 00:01:19.120 --> 00:01:22.200 and then you start off with some brackets. 32 00:01:22.200 --> 00:01:27.200 You put here the value that you want to use in the new list, 33 00:01:27.390 --> 00:01:29.203 which would be number times two, 34 00:01:30.280 --> 00:01:32.980 and then you put in something like 35 00:01:32.980 --> 00:01:34.653 what you see in a for loop. 36 00:01:36.890 --> 00:01:39.060 So this is a shorter way of doing 37 00:01:39.060 --> 00:01:41.730 exactly this piece of code here. 38 00:01:41.730 --> 00:01:45.820 You say, I wanna put in my new list number times two 39 00:01:45.820 --> 00:01:50.430 where the number is each number is the numbers list, 40 00:01:50.430 --> 00:01:53.960 and the syntax for that is again inside square brackets, 41 00:01:53.960 --> 00:01:56.720 the value you want to create into the new list, 42 00:01:56.720 --> 00:01:59.860 for the variable that you want to use, 43 00:01:59.860 --> 00:02:02.410 in the list that you want to iterate over 44 00:02:02.410 --> 00:02:04.363 to create this new list. 45 00:02:05.450 --> 00:02:07.790 So again, it does exactly the same as this. 46 00:02:07.790 --> 00:02:09.210 You create your new list. 47 00:02:09.210 --> 00:02:10.800 It's right over the numbers list, 48 00:02:10.800 --> 00:02:14.370 and add the new number into the new list 49 00:02:14.370 --> 00:02:16.830 so you can just delete this and of course 50 00:02:16.830 --> 00:02:20.720 move the print statement underneath so that it exists 51 00:02:20.720 --> 00:02:24.050 when you print it and the output is exactly the same. 52 00:02:24.050 --> 00:02:25.990 Now remember that instead of creating 53 00:02:25.990 --> 00:02:28.640 your own list of numbers, you can use range, 54 00:02:28.640 --> 00:02:31.310 so you can say in range five. 55 00:02:31.310 --> 00:02:32.520 Now this is exactly the same. 56 00:02:32.520 --> 00:02:35.460 You're still creating your new number variable 57 00:02:35.460 --> 00:02:39.360 that only exists within this new list, 58 00:02:39.360 --> 00:02:42.360 and you're iterating from zero to five, 59 00:02:42.360 --> 00:02:43.460 and every iteration, 60 00:02:43.460 --> 00:02:47.290 you're putting into your new list number times two. 61 00:02:47.290 --> 00:02:49.580 Notice that here, you can put whatever you want, 62 00:02:49.580 --> 00:02:51.250 so it doesn't have to be number times two. 63 00:02:51.250 --> 00:02:53.760 It could just be five if you want, 64 00:02:53.760 --> 00:02:55.380 and then what you would get out 65 00:02:55.380 --> 00:02:57.300 would be five, five, five, five 66 00:02:57.300 --> 00:02:59.600 because you are repeating five times, 67 00:02:59.600 --> 00:03:02.520 and the value you're putting each time is five. 68 00:03:02.520 --> 00:03:05.580 If you want, you can go back to number times two, 69 00:03:05.580 --> 00:03:06.810 but again, just wanted to tell you 70 00:03:06.810 --> 00:03:08.790 that anything goes in here. 71 00:03:08.790 --> 00:03:11.070 You don't have to use this variable. 72 00:03:11.070 --> 00:03:12.770 You can put whatever you want, 73 00:03:12.770 --> 00:03:14.800 and like I mentioned earlier on in the course, 74 00:03:14.800 --> 00:03:16.990 if you're not using the variable, 75 00:03:16.990 --> 00:03:20.270 you normally use the underscore for it to signal 76 00:03:20.270 --> 00:03:23.173 that you don't care about the variable in here. 77 00:03:24.220 --> 00:03:27.230 Let's say you've got a list with your friends' ages, 78 00:03:27.230 --> 00:03:29.950 and you want to create a new list which contains 79 00:03:29.950 --> 00:03:33.110 a string describing your friend's age 80 00:03:33.110 --> 00:03:35.120 for each of your friends. 81 00:03:35.120 --> 00:03:37.890 So you could create something like age strings, 82 00:03:37.890 --> 00:03:39.960 and in here, put an f-string. 83 00:03:39.960 --> 00:03:44.200 My friend is age years old. 84 00:03:44.200 --> 00:03:47.590 Now you want to create this string 85 00:03:47.590 --> 00:03:50.030 for each element in this list, 86 00:03:50.030 --> 00:03:54.800 so you would say for age in friend ages. 87 00:03:54.800 --> 00:03:57.330 Let me just make this a bit bigger for you 88 00:03:57.330 --> 00:03:59.290 so you can see it in one line. 89 00:03:59.290 --> 00:04:02.390 So this is exactly the same structure as we had before. 90 00:04:02.390 --> 00:04:04.510 You've got your brackets that signal 91 00:04:04.510 --> 00:04:06.270 that you're creating a new list, 92 00:04:06.270 --> 00:04:07.990 and then because of what's inside it, 93 00:04:07.990 --> 00:04:09.350 you know it's a list comprehension. 94 00:04:09.350 --> 00:04:11.240 You've got this for loop there, 95 00:04:11.240 --> 00:04:13.350 and what you're putting into your new list 96 00:04:13.350 --> 00:04:17.300 is this string, my friend is age years old, 97 00:04:17.300 --> 00:04:19.830 and then you're creating the age variable 98 00:04:19.830 --> 00:04:22.600 that you're gonna use for each of the values 99 00:04:22.600 --> 00:04:24.433 in your friend ages list. 100 00:04:25.290 --> 00:04:28.420 So you end up with one string for each age 101 00:04:28.420 --> 00:04:32.040 where the age variable would be replaced into there. 102 00:04:32.040 --> 00:04:35.700 So you can print age strings. 103 00:04:35.700 --> 00:04:37.570 So you can see the output here is a list 104 00:04:37.570 --> 00:04:40.427 with four elements, four strings, one for each age. 105 00:04:41.720 --> 00:04:44.550 Now let's say you've got the names of some of your friends 106 00:04:44.550 --> 00:04:46.850 like Rolf, Bob, and Jen, 107 00:04:46.850 --> 00:04:49.030 and what you want is to apply a transformation 108 00:04:49.030 --> 00:04:51.790 to each element so that you'll only get back 109 00:04:51.790 --> 00:04:53.050 lower-case letters, 110 00:04:53.050 --> 00:04:57.370 so Rolf will be turned to lower-case r-o-l-f, 111 00:04:57.370 --> 00:04:59.490 Bob will get a lower-case b, 112 00:04:59.490 --> 00:05:02.100 and Jen will get a lower-case j. 113 00:05:02.100 --> 00:05:03.600 What you can do is you can create a new list 114 00:05:03.600 --> 00:05:05.070 which will be a list comprehension, 115 00:05:05.070 --> 00:05:08.290 and what you'll do here is name dot lower 116 00:05:08.290 --> 00:05:11.090 which actually can be used on any string 117 00:05:11.090 --> 00:05:16.090 to turn it into a lower-case string for name in names. 118 00:05:16.910 --> 00:05:19.320 And again, you create the name variable 119 00:05:19.320 --> 00:05:23.310 which will be each of the strings inside the names list, 120 00:05:23.310 --> 00:05:26.630 and you're putting into your new list name dot lower, 121 00:05:26.630 --> 00:05:29.390 every iteration, which will give you lower-case Rolf, 122 00:05:29.390 --> 00:05:31.560 lower-case Bob, lower-case Jen. 123 00:05:31.560 --> 00:05:32.970 So if we print this out, 124 00:05:32.970 --> 00:05:35.150 you'll see the output there. 125 00:05:35.150 --> 00:05:36.680 This is particularly useful for when 126 00:05:36.680 --> 00:05:39.140 you want to work with user input. 127 00:05:39.140 --> 00:05:41.950 For example, let's say you've got a list of friend names, 128 00:05:41.950 --> 00:05:43.610 and you want the user to give you their name 129 00:05:43.610 --> 00:05:45.390 so you can see if they're in the list. 130 00:05:45.390 --> 00:05:46.713 It's possible that the user will type 131 00:05:46.713 --> 00:05:49.350 lower-case or upper-case letters 132 00:05:49.350 --> 00:05:51.750 which don't exactly match these strings. 133 00:05:51.750 --> 00:05:54.240 So it can be useful to turn everything to lower-case 134 00:05:54.240 --> 00:05:55.400 when you're doing comparisons 135 00:05:55.400 --> 00:05:58.600 between your strings and user input. 136 00:05:58.600 --> 00:05:59.960 For example, something like this. 137 00:05:59.960 --> 00:06:03.180 You've got a friend variable which is some user input. 138 00:06:03.180 --> 00:06:05.100 We ask them to enter their friend name, 139 00:06:05.100 --> 00:06:06.700 and then you've got a friends list 140 00:06:06.700 --> 00:06:09.130 which contains a bunch of friends. 141 00:06:09.130 --> 00:06:12.320 If you wanna see whether the user's input is in this list, 142 00:06:12.320 --> 00:06:16.470 you can do something like if friend in friends, 143 00:06:16.470 --> 00:06:19.520 print friend is one of your friends. 144 00:06:19.520 --> 00:06:20.733 So we can run this, 145 00:06:22.030 --> 00:06:24.100 and if we enter Rolf say, 146 00:06:24.100 --> 00:06:26.200 then we'll get Rolf is one of your friends, 147 00:06:26.200 --> 00:06:29.390 but very often users will enter ROLF 148 00:06:29.390 --> 00:06:31.160 or maybe lower-case rolf. 149 00:06:31.160 --> 00:06:34.160 Maybe they can't be bothered entering the capital R, 150 00:06:34.160 --> 00:06:36.500 or maybe their name is all in lower-case 151 00:06:36.500 --> 00:06:37.720 as in some countries. 152 00:06:37.720 --> 00:06:39.090 That is what happens. 153 00:06:39.090 --> 00:06:41.500 So if we enter rolf with a lower-case r, 154 00:06:41.500 --> 00:06:43.010 you see that nothing gets printed out 155 00:06:43.010 --> 00:06:45.480 even though it probably should. 156 00:06:45.480 --> 00:06:48.840 So what to do is create a new list 157 00:06:48.840 --> 00:06:51.250 where each of the names is lower-case. 158 00:06:51.250 --> 00:06:55.000 So friends lower can be named all lower 159 00:06:55.000 --> 00:06:56.823 for name in friends, 160 00:06:58.030 --> 00:07:00.618 and then, what we're gonna do is compare friend 161 00:07:00.618 --> 00:07:03.360 against friends lower, 162 00:07:03.360 --> 00:07:05.710 but if we do this and run it as-is, 163 00:07:05.710 --> 00:07:07.850 when we type Rolf with a capital R, 164 00:07:07.850 --> 00:07:10.740 it will no longer show as correct because now 165 00:07:10.740 --> 00:07:15.460 we are comparing capital R Rolf with lower-case rolf 166 00:07:15.460 --> 00:07:17.260 which is in this list. 167 00:07:17.260 --> 00:07:20.240 So we also have to turn friend into lower-case 168 00:07:20.240 --> 00:07:22.290 in order to have a fair comparison, 169 00:07:22.290 --> 00:07:26.680 so we'll do if friend dot lower in friends lower, 170 00:07:26.680 --> 00:07:28.750 then we will print out. 171 00:07:28.750 --> 00:07:31.910 This has a benefit which is that now I can type Rolf 172 00:07:31.910 --> 00:07:33.190 with a capital R, 173 00:07:33.190 --> 00:07:35.010 and it says that it's one of my friends, 174 00:07:35.010 --> 00:07:37.750 but I can also type rolf with a lower-case r, 175 00:07:37.750 --> 00:07:39.950 and that's also fine. 176 00:07:39.950 --> 00:07:42.170 Finally, I can type Rolf with any sort 177 00:07:42.170 --> 00:07:45.930 of upper or lower-case letters, and it will be correct. 178 00:07:45.930 --> 00:07:48.184 By the way, if you wanted to print this out 179 00:07:48.184 --> 00:07:51.610 with the usual capitalization which is 180 00:07:51.610 --> 00:07:54.570 the R upper-case and rest of the letters lower-case, 181 00:07:54.570 --> 00:07:56.640 then that's called title casing, 182 00:07:56.640 --> 00:08:01.190 and you can do that on the friend here by doing title. 183 00:08:01.190 --> 00:08:04.090 So here I've typed ROLF with all upper-case letters, 184 00:08:04.090 --> 00:08:07.020 but because we are printing friend dot title, 185 00:08:07.020 --> 00:08:08.730 what that does is it turns it 186 00:08:08.730 --> 00:08:11.350 into the traditional title casing 187 00:08:11.350 --> 00:08:14.090 which is that every word in the string 188 00:08:14.090 --> 00:08:16.380 will have the first letter as upper-case 189 00:08:16.380 --> 00:08:18.490 and the other letters as lower-case. 190 00:08:18.490 --> 00:08:20.440 So this can be just a small improvement, 191 00:08:20.440 --> 00:08:23.100 and of course you can put this into a separate variable 192 00:08:23.100 --> 00:08:25.410 if you prefer just like that. 193 00:08:25.410 --> 00:08:27.750 So either goes; it's up to you. 194 00:08:27.750 --> 00:08:28.730 So that's it for this video. 195 00:08:28.730 --> 00:08:31.770 We have learned about list comprehensions which, to recap, 196 00:08:31.770 --> 00:08:34.030 have a structure of the new value 197 00:08:34.030 --> 00:08:36.040 that you wanna put into your list 198 00:08:36.040 --> 00:08:40.310 for the variable that you wanna use for each iteration 199 00:08:40.310 --> 00:08:43.360 in the interable that you wanna go over, 200 00:08:43.360 --> 00:08:45.170 and this doesn't have to be a list by the way. 201 00:08:45.170 --> 00:08:48.000 It could be a couple or a set or anything else. 202 00:08:48.000 --> 00:08:48.833 In the next videos, 203 00:08:48.833 --> 00:08:51.080 we're going to learn a bit more about list comprehensions, 204 00:08:51.080 --> 00:08:53.460 including how to add conditionals to them 205 00:08:53.460 --> 00:08:55.440 to make them a bit more flexible. 206 00:08:55.440 --> 00:08:56.450 Thank you for joining me, 207 00:08:56.450 --> 00:08:58.200 and I'll see you in the next video.