WEBVTT 1 00:00:00.250 --> 00:00:01.810 Hi guys, and welcome back. 2 00:00:01.810 --> 00:00:05.580 In this video, I wanted to tell you about the zip function. 3 00:00:05.580 --> 00:00:07.440 In the last video, we saw this code 4 00:00:07.440 --> 00:00:10.680 where we have our friends and how long ago we saw them. 5 00:00:10.680 --> 00:00:13.070 And let's say we wanted to create a dictionary 6 00:00:13.070 --> 00:00:17.670 of the friend names to the time since we last saw them. 7 00:00:17.670 --> 00:00:19.860 We saw how we could add a conditional here 8 00:00:19.860 --> 00:00:22.960 if we wanted to filter out friends that we haven't seen 9 00:00:22.960 --> 00:00:24.570 for five days or more. 10 00:00:24.570 --> 00:00:27.360 But let's say that we don't want to do that. 11 00:00:27.360 --> 00:00:29.840 If you want to create a dictionary like this, 12 00:00:29.840 --> 00:00:32.340 you actually don't have to run this code. 13 00:00:32.340 --> 00:00:34.410 You can run something a bit simpler. 14 00:00:34.410 --> 00:00:36.670 If you manage to combine these two lists 15 00:00:36.670 --> 00:00:38.960 into one list with this format, 16 00:00:38.960 --> 00:00:41.980 where you've got a pair of elements 17 00:00:41.980 --> 00:00:44.113 as each element of the list, 18 00:00:45.270 --> 00:00:48.790 then you can just pass this through the dict function 19 00:00:48.790 --> 00:00:50.700 as we've seen earlier on in the course 20 00:00:50.700 --> 00:00:52.173 in order to get a dictionary. 21 00:00:53.160 --> 00:00:55.310 But how do you combine these two lists 22 00:00:55.310 --> 00:00:57.230 into something like this? 23 00:00:57.230 --> 00:01:00.640 Well, that's where the zip function comes into play. 24 00:01:00.640 --> 00:01:04.690 You can do zip friends and time_since_seen, 25 00:01:04.690 --> 00:01:07.970 and that is essentially going to run a zipper 26 00:01:07.970 --> 00:01:10.470 through both of these lists, closing them up 27 00:01:10.470 --> 00:01:13.050 and combining them into one list 28 00:01:13.050 --> 00:01:18.050 where each element of the new list is a tuple of elements. 29 00:01:18.100 --> 00:01:19.800 So that's gonna be Rolf and 3, 30 00:01:19.800 --> 00:01:22.543 Bob and 7, Jen and 15, Anne and 11. 31 00:01:23.610 --> 00:01:25.710 So you can actually just use this code 32 00:01:25.710 --> 00:01:27.640 instead of this list comprehension, 33 00:01:27.640 --> 00:01:31.070 and it will be a little bit simpler, just like that. 34 00:01:31.070 --> 00:01:33.880 Notice that if we turn this into a list 35 00:01:33.880 --> 00:01:36.980 and print it out, you'll see that zip turns them into a list 36 00:01:36.980 --> 00:01:38.493 of tuples like this. 37 00:01:40.180 --> 00:01:42.680 As an interesting aside, if you just print out the result 38 00:01:42.680 --> 00:01:46.230 of calling zip, you won't see something like that. 39 00:01:46.230 --> 00:01:48.830 You'll see something a bit more confusing. 40 00:01:48.830 --> 00:01:51.260 We're going to be learning more about these things 41 00:01:51.260 --> 00:01:53.720 as we go through the course, but for now just know 42 00:01:53.720 --> 00:01:56.410 that zip essentially combines two lists 43 00:01:56.410 --> 00:01:59.070 or more into one list. 44 00:01:59.070 --> 00:02:02.210 I say or more, because you can have more lists here 45 00:02:02.210 --> 00:02:03.400 if you want. 46 00:02:03.400 --> 00:02:05.940 So if we do something like this 47 00:02:05.940 --> 00:02:07.530 and turn it into a list, you'll see 48 00:02:07.530 --> 00:02:10.880 that now the tuples have three elements. 49 00:02:10.880 --> 00:02:13.400 Rolf, 3, 1. 50 00:02:13.400 --> 00:02:15.610 Bob, 7, 2. 51 00:02:15.610 --> 00:02:18.700 Jen, 15, 3, and so on. 52 00:02:18.700 --> 00:02:22.440 Do note that this list was longer than these two lists. 53 00:02:22.440 --> 00:02:24.660 It has five elements instead of four, 54 00:02:24.660 --> 00:02:27.290 but zip will just ignore any elements 55 00:02:27.290 --> 00:02:29.640 that don't match the other lists. 56 00:02:29.640 --> 00:02:32.370 So you will always end up with something 57 00:02:32.370 --> 00:02:34.730 like the shortest list. 58 00:02:34.730 --> 00:02:36.090 Thank you for joining me in this video. 59 00:02:36.090 --> 00:02:37.360 I hope you've learned something, 60 00:02:37.360 --> 00:02:39.010 and I'll see you in the next one.