WEBVTT 1 00:00:00.130 --> 00:00:01.850 Hi guys, and welcome back. 2 00:00:01.850 --> 00:00:03.110 In this video, I wanted to tell you 3 00:00:03.110 --> 00:00:06.180 about set and dictionary comprehensions. 4 00:00:06.180 --> 00:00:08.440 In the last video, we had a look at this code 5 00:00:08.440 --> 00:00:12.010 where we have our friends list and our guests list. 6 00:00:12.010 --> 00:00:13.640 And in order to compare them both, 7 00:00:13.640 --> 00:00:17.270 we tried to turn them both into lower case 8 00:00:17.270 --> 00:00:19.070 and then pass them through sets, 9 00:00:19.070 --> 00:00:21.560 so that we could then print the intersection 10 00:00:21.560 --> 00:00:24.707 when we did friends_lower.intersection(guests_lower). 11 00:00:26.760 --> 00:00:31.290 However, we can just use set comprehensions 12 00:00:31.290 --> 00:00:33.340 instead of list comprehensions 13 00:00:33.340 --> 00:00:36.090 and then pass it through the set function. 14 00:00:36.090 --> 00:00:38.000 Just like we can do list comprehensions 15 00:00:38.000 --> 00:00:39.710 by using square brackets 16 00:00:39.710 --> 00:00:41.670 and then putting the thing we wanna put 17 00:00:41.670 --> 00:00:45.040 into the new list for the iterating variable 18 00:00:45.040 --> 00:00:47.960 in the iterable, we can just do this. 19 00:00:47.960 --> 00:00:50.970 Instead of creating a new list first 20 00:00:50.970 --> 00:00:52.900 and then making a set out of it, 21 00:00:52.900 --> 00:00:54.940 we can just create a set straight away, 22 00:00:54.940 --> 00:00:56.780 and that is going to be faster, 23 00:00:56.780 --> 00:00:58.530 and it'll sure be more readable. 24 00:00:58.530 --> 00:01:00.540 Let's do the same down here. 25 00:01:00.540 --> 00:01:03.280 Now one of the problems we had in the last video 26 00:01:03.280 --> 00:01:05.780 with doing something like this is we weren't able 27 00:01:05.780 --> 00:01:09.590 to output the names in capitalised format. 28 00:01:09.590 --> 00:01:11.900 So we always had the lowercase names at the end, 29 00:01:11.900 --> 00:01:15.820 because that's what we used to sort of compare them. 30 00:01:15.820 --> 00:01:18.560 But actually, we can create a final set comprehension 31 00:01:18.560 --> 00:01:20.645 that does this for us. 32 00:01:20.645 --> 00:01:23.312 (keys clicking) 33 00:01:26.070 --> 00:01:29.180 So what I've got here is a third set comprehension 34 00:01:29.180 --> 00:01:32.180 that puts the name turned into title case 35 00:01:32.180 --> 00:01:34.973 for the name in the intersection. 36 00:01:35.910 --> 00:01:38.570 Of course, it is easier if you take this out 37 00:01:38.570 --> 00:01:41.471 and put it into another variable 38 00:01:41.471 --> 00:01:43.670 (keys clicking) 39 00:01:43.670 --> 00:01:44.910 just like that. 40 00:01:44.910 --> 00:01:46.360 If we run this, you'll see 41 00:01:46.360 --> 00:01:48.680 that the output is now what we wanted. 42 00:01:48.680 --> 00:01:51.370 Though it is a little bit longer, 43 00:01:51.370 --> 00:01:53.280 it does have the extra benefit 44 00:01:53.280 --> 00:01:56.070 of removing any duplicate friends if there were any. 45 00:01:56.070 --> 00:01:58.120 So if somebody was in this list twice, 46 00:01:58.120 --> 00:02:00.700 for example, for some reason, either due to an error, 47 00:02:00.700 --> 00:02:03.830 or maybe they RSVP'd twice or something like that, 48 00:02:03.830 --> 00:02:05.960 then this would get rid of that. 49 00:02:05.960 --> 00:02:07.820 However, you could also have the problem 50 00:02:07.820 --> 00:02:09.670 of removing duplicate (laughs) elements, 51 00:02:09.670 --> 00:02:12.170 so if two people have the same name, you could end up 52 00:02:12.170 --> 00:02:15.460 with only one of those people showing up in these sets. 53 00:02:15.460 --> 00:02:16.810 Either way, I wanted to tell you 54 00:02:16.810 --> 00:02:19.350 that you can do set comprehensions, 55 00:02:19.350 --> 00:02:21.290 which is the same thing as a list comprehension, 56 00:02:21.290 --> 00:02:24.730 but using curly braces instead of square brackets. 57 00:02:24.730 --> 00:02:27.223 And you end up with a set at the end of it. 58 00:02:28.640 --> 00:02:31.030 Let's say you've got a list of friend names 59 00:02:31.030 --> 00:02:33.910 and how long ago since you last saw them, 60 00:02:33.910 --> 00:02:36.650 and you want to create a dictionary out of these two. 61 00:02:36.650 --> 00:02:38.230 What you can do is you can create something 62 00:02:38.230 --> 00:02:40.820 like a variable called long_timers. 63 00:02:40.820 --> 00:02:43.250 And once again using curly braces, 64 00:02:43.250 --> 00:02:45.080 just as we do with dictionaries, 65 00:02:45.080 --> 00:02:47.847 you can do friends(i): time_since_seen(i). 66 00:02:51.620 --> 00:02:55.370 And what this does is it accesses the i-th element 67 00:02:55.370 --> 00:02:56.470 of the friends list. 68 00:02:56.470 --> 00:02:59.050 So for example if i was zero, 69 00:02:59.050 --> 00:03:02.890 it would access friends zero, and that's Rolf. 70 00:03:02.890 --> 00:03:06.630 And it would associate with that this value here, 71 00:03:06.630 --> 00:03:09.960 which is time_since_seen zero, which would be 3, 72 00:03:09.960 --> 00:03:14.360 so you could end up with Rolf, 3. 73 00:03:14.360 --> 00:03:18.010 Now if you repeat this for every index, 74 00:03:18.010 --> 00:03:21.230 so zero, one, two, and three, 75 00:03:21.230 --> 00:03:24.030 you'd end up with a dictionary of the elements, 76 00:03:24.030 --> 00:03:27.080 which include all of these in these two lists. 77 00:03:27.080 --> 00:03:32.080 So you could do for i in range of the length of friends. 78 00:03:33.240 --> 00:03:35.950 And what this means is you are creating a new variable 79 00:03:35.950 --> 00:03:39.950 called i, and its values will range from zero, 80 00:03:39.950 --> 00:03:41.881 which is the start of the range function, 81 00:03:41.881 --> 00:03:45.900 up to the length of friends, which is four. 82 00:03:45.900 --> 00:03:48.620 So we would go from zero to three, inclusive, 83 00:03:48.620 --> 00:03:50.783 and that would give you all of these. 84 00:03:51.810 --> 00:03:53.530 If we print this out and run it, 85 00:03:53.530 --> 00:03:55.130 you'll see what I mean. 86 00:03:55.130 --> 00:04:00.130 You get Rolf and 3, Bob and 7, Jen and 15, Anne and 11. 87 00:04:00.470 --> 00:04:02.480 One of the benefits of doing a list comprehension 88 00:04:02.480 --> 00:04:04.540 like this one is that if you were only interested 89 00:04:04.540 --> 00:04:07.670 in those people that you haven't seen for five days or more, 90 00:04:07.670 --> 00:04:10.984 you can of course put a conditional in here, as we've seen. 91 00:04:10.984 --> 00:04:13.420 (keys clicking) 92 00:04:13.420 --> 00:04:14.630 Something like that. 93 00:04:14.630 --> 00:04:15.890 So now you're only running this 94 00:04:15.890 --> 00:04:20.890 if the time_since_seen(i), which would be the number 95 00:04:20.980 --> 00:04:23.740 that we're currently looking at, is greater than five. 96 00:04:23.740 --> 00:04:26.830 So now you would only get Bob, Jen, and Anne. 97 00:04:26.830 --> 00:04:30.070 Running this, you'll see that that is correct. 98 00:04:30.070 --> 00:04:32.680 So dictionary comprehension, which is this, 99 00:04:32.680 --> 00:04:35.140 works in exactly the same way as set comprehension, 100 00:04:35.140 --> 00:04:36.960 in that you use the calibrators, 101 00:04:36.960 --> 00:04:38.690 but instead of putting a single value 102 00:04:38.690 --> 00:04:40.430 into the set comprehension, 103 00:04:40.430 --> 00:04:43.890 you put the key, colon, the value, 104 00:04:43.890 --> 00:04:46.710 just as we did with dictionaries before. 105 00:04:46.710 --> 00:04:49.570 Dictionary comprehension is used much less 106 00:04:49.570 --> 00:04:51.380 than other types of comprehension, 107 00:04:51.380 --> 00:04:53.150 but sometimes it can be really handy. 108 00:04:53.150 --> 00:04:55.000 So I just wanted to show it to you, 109 00:04:55.000 --> 00:04:55.990 tell you that it's possible, 110 00:04:55.990 --> 00:04:58.710 and maybe you'll encounter it in the future. 111 00:04:58.710 --> 00:05:00.010 Thank you for joining me in this video. 112 00:05:00.010 --> 00:05:01.200 I hope you've learned something, 113 00:05:01.200 --> 00:05:02.850 and I'll see you in the next one.