WEBVTT 1 00:00:00.270 --> 00:00:01.770 Hi guys and welcome back. 2 00:00:01.770 --> 00:00:03.490 In this video we're going to be learning 3 00:00:03.490 --> 00:00:06.210 about list comprehensions with conditionals, 4 00:00:06.210 --> 00:00:09.020 which we can use to figure out some elements 5 00:00:09.020 --> 00:00:11.460 while we're putting them into a new list 6 00:00:11.460 --> 00:00:13.130 using list comprehensions. 7 00:00:13.130 --> 00:00:16.000 So let's say you've got some ages of your friends 8 00:00:16.000 --> 00:00:19.960 and you to get only the odd ages. 9 00:00:19.960 --> 00:00:23.665 You could do something like, odds equal 10 00:00:23.665 --> 00:00:27.070 age for age in ages. 11 00:00:27.070 --> 00:00:29.800 And that is a list comprehension that puts the value 12 00:00:29.800 --> 00:00:33.670 age for age, which is the new variable 13 00:00:33.670 --> 00:00:36.010 you're creating, in the Age's List. 14 00:00:36.010 --> 00:00:38.820 So it's going to put every age in the Age's List 15 00:00:38.820 --> 00:00:41.863 into a new list that you're calling Odds. 16 00:00:42.840 --> 00:00:45.280 What you can do is if you wanted to get only 17 00:00:45.280 --> 00:00:48.590 the odd ages, you can actually put an if statement 18 00:00:48.590 --> 00:00:52.703 at the end, if age modulo two equal one. 19 00:00:53.740 --> 00:00:55.800 And what this is gonna do is it's going to put 20 00:00:55.800 --> 00:01:00.800 this value into the new list if this conditional 21 00:01:00.850 --> 00:01:03.340 is true, and if this conditional is false 22 00:01:03.340 --> 00:01:05.350 then it's not going to put the value in. 23 00:01:05.350 --> 00:01:08.400 So if we run that you'll see 35, 27, and 21, 24 00:01:08.400 --> 00:01:11.630 which are the odd ages in the original list. 25 00:01:11.630 --> 00:01:13.510 Now I appreciate as soon as you start 26 00:01:13.510 --> 00:01:16.290 putting conditionals into a list comprehension, 27 00:01:16.290 --> 00:01:19.080 it does get a little bit more cluttered 28 00:01:19.080 --> 00:01:22.030 because you have the for keyword, 29 00:01:22.030 --> 00:01:26.230 in keyword, and if keyword, all in one line. 30 00:01:26.230 --> 00:01:28.350 So something you have to be careful with when you're writing 31 00:01:28.350 --> 00:01:30.500 list comprehensions, is that if it's getting 32 00:01:30.500 --> 00:01:33.900 too long and too complicated, you can split 33 00:01:33.900 --> 00:01:36.590 things out and move them outside the list comprehension, 34 00:01:36.590 --> 00:01:39.290 or you can just use a normal for loop, 35 00:01:39.290 --> 00:01:42.380 like we learned in the last video to add to a new list. 36 00:01:42.380 --> 00:01:45.240 That can reduce the cognitive load on your brain 37 00:01:45.240 --> 00:01:47.400 because it can make the code a bit easier to read. 38 00:01:47.400 --> 00:01:50.270 So list comprehensions are good when they are kept simple 39 00:01:50.270 --> 00:01:52.910 and they help your code, but if they're too complicated 40 00:01:52.910 --> 00:01:54.973 they can actually detract from your code. 41 00:01:56.760 --> 00:02:00.190 Let's say you've got the names of some friends 42 00:02:00.190 --> 00:02:03.570 and they are in a mix of uppercase and lowercase 43 00:02:03.570 --> 00:02:06.730 spelling, and then you've got another list of guests 44 00:02:06.730 --> 00:02:09.380 that have come to one of your parties. 45 00:02:09.380 --> 00:02:12.360 We're going to turn every friend in our friends list 46 00:02:12.360 --> 00:02:14.930 into lowercase, so that we can easily compare them 47 00:02:14.930 --> 00:02:17.200 with our guests. 48 00:02:17.200 --> 00:02:21.190 But if we wanted to compare, now let's say 49 00:02:22.660 --> 00:02:26.040 we want a final list of those friends 50 00:02:26.040 --> 00:02:28.460 that have come to your party. 51 00:02:28.460 --> 00:02:31.150 So there are two approaches. 52 00:02:31.150 --> 00:02:33.630 The first approach is that you can turn 53 00:02:33.630 --> 00:02:36.480 all of these guests into lowercase as well. 54 00:02:36.480 --> 00:02:40.950 So you can go guests_lower equal g_lower 55 00:02:40.950 --> 00:02:43.330 for g in guests. 56 00:02:43.330 --> 00:02:46.240 So now you have two lowercase lists. 57 00:02:46.240 --> 00:02:48.680 If you turn both of these into sets 58 00:02:48.680 --> 00:02:51.353 by actually passing them through the set function, 59 00:02:53.500 --> 00:02:56.710 then you could print the intersection of the two. 60 00:02:56.710 --> 00:03:01.710 So friends_lower.intersection with guests_lower, 61 00:03:02.010 --> 00:03:03.470 and this will give you those friends 62 00:03:03.470 --> 00:03:08.470 that are here and also here, which are Charlie and Rolf. 63 00:03:08.600 --> 00:03:12.360 So this is one way to do it, but it's a bit more 64 00:03:12.360 --> 00:03:16.310 difficult to get these names out as capital C 65 00:03:16.310 --> 00:03:19.330 and capital R because in the sets you've got them 66 00:03:19.330 --> 00:03:20.373 all as lowercase. 67 00:03:21.350 --> 00:03:22.930 So instead we're not going to use sets 68 00:03:22.930 --> 00:03:24.420 for our approach, and we're going to use 69 00:03:24.420 --> 00:03:28.350 a list comprehension where we create a present_friends list, 70 00:03:28.350 --> 00:03:30.340 because it's going to be a long list comprehension, 71 00:03:30.340 --> 00:03:33.220 I'm going to split it out into multiple lines. 72 00:03:33.220 --> 00:03:35.940 And you can do this, you can just put the square brackets 73 00:03:35.940 --> 00:03:38.410 in one line, and in another, and in the middle you can 74 00:03:38.410 --> 00:03:40.410 write out your list comprehension. 75 00:03:40.410 --> 00:03:41.700 And what I'm going to do is I'm going 76 00:03:41.700 --> 00:03:46.700 to put the name for name in guests if the name.lower 77 00:03:48.140 --> 00:03:50.050 is in friends_lower. 78 00:03:51.040 --> 00:03:53.900 And what this allows me to do is put each of the names 79 00:03:53.900 --> 00:03:58.900 of the guests inside my new list, if that name 80 00:03:59.270 --> 00:04:03.970 turned to lowercase, is in my friends_lower list. 81 00:04:03.970 --> 00:04:05.910 And of course, instead of just name, 82 00:04:05.910 --> 00:04:08.180 I can do name.title. 83 00:04:08.180 --> 00:04:12.240 If we print this out, you'll see the output here, 84 00:04:12.240 --> 00:04:14.610 which is Rolf and Charlie. 85 00:04:14.610 --> 00:04:17.110 Another benefit of this that might be a benefit 86 00:04:17.110 --> 00:04:19.320 or it might be irrelevant for your programme, 87 00:04:19.320 --> 00:04:22.930 is that the output is in order of the guests. 88 00:04:22.930 --> 00:04:24.980 So Rolf and then Charlie. 89 00:04:24.980 --> 00:04:27.500 When you turn these into sets they lose the order. 90 00:04:27.500 --> 00:04:29.940 So potentially if this order had some sort of meaning 91 00:04:29.940 --> 00:04:32.250 in your programme, such as who arrived first, 92 00:04:32.250 --> 00:04:34.400 or something like that, then you would get to keep 93 00:04:34.400 --> 00:04:36.800 that if you use lists, but you wouldn't get to keep 94 00:04:36.800 --> 00:04:38.510 that if you use sets. 95 00:04:38.510 --> 00:04:40.480 As an aside, here's something that you probably 96 00:04:40.480 --> 00:04:44.390 should never do, which is nest list comprehensions 97 00:04:44.390 --> 00:04:45.490 one inside of another. 98 00:04:46.520 --> 00:04:49.230 Here you've got friends_lower, which you know 99 00:04:49.230 --> 00:04:51.570 is this list comprehension here, which turns 100 00:04:51.570 --> 00:04:54.850 each friend into their lowercase variant. 101 00:04:54.850 --> 00:04:56.770 But you actually don't have to define a variable, 102 00:04:56.770 --> 00:04:58.750 you can just copy it and paste it in there, 103 00:04:58.750 --> 00:05:01.300 and delete this variable entirely. 104 00:05:01.300 --> 00:05:04.110 But as you can see, this is a very dangerous 105 00:05:04.110 --> 00:05:08.150 thing to be doing, cause it quickly becomes unreadable. 106 00:05:08.150 --> 00:05:11.260 Splitting this out into variables is almost always 107 00:05:11.260 --> 00:05:13.260 the better choice because it's going to make 108 00:05:13.260 --> 00:05:15.110 your code more readable. 109 00:05:15.110 --> 00:05:18.090 If you do have a long list comprehension, 110 00:05:18.090 --> 00:05:20.730 where one line is not enough, you can split 111 00:05:20.730 --> 00:05:23.113 it further into something like this. 112 00:05:25.130 --> 00:05:28.870 It's usual to have the first line as the new thing 113 00:05:28.870 --> 00:05:31.210 that you want to put into your list. 114 00:05:31.210 --> 00:05:33.970 The second line as the iteration over 115 00:05:33.970 --> 00:05:36.540 an existing iterable like this list. 116 00:05:36.540 --> 00:05:39.433 And finally the comprehension, if there is one. 117 00:05:40.800 --> 00:05:43.100 Splitting list comprehensions in this way 118 00:05:43.100 --> 00:05:45.380 can be helpful to make your code a bit more readable, 119 00:05:45.380 --> 00:05:48.130 but it is a sign that it is getting a bit too long, 120 00:05:48.130 --> 00:05:50.570 so it's almost always better to put this here 121 00:05:50.570 --> 00:05:54.400 to a separate variable, and either keep it in three lines, 122 00:05:54.400 --> 00:05:56.820 or put it back into one line. 123 00:05:56.820 --> 00:05:59.150 That's it for this video though, I just wanted to show you 124 00:05:59.150 --> 00:06:02.200 that you can put if statements inside the list comprehension 125 00:06:02.200 --> 00:06:03.370 where it makes sense. 126 00:06:03.370 --> 00:06:05.000 Don't overcomplicate things. 127 00:06:05.000 --> 00:06:07.550 Writing simple code is almost always better 128 00:06:07.550 --> 00:06:09.410 than writing complicate code. 129 00:06:09.410 --> 00:06:10.900 Thanks for joining me in this video 130 00:06:10.900 --> 00:06:12.550 and I'll see you in the next one.