WEBVTT 1 00:00:00.150 --> 00:00:01.820 Hi guys, and welcome back. 2 00:00:01.820 --> 00:00:04.940 In this video we're going to learn about destructuring. 3 00:00:04.940 --> 00:00:07.450 Sounds complicated but it's actually really straightforward, 4 00:00:07.450 --> 00:00:09.040 and it's gonna come in really handy. 5 00:00:09.040 --> 00:00:11.730 It makes our programmes much more easy to read. 6 00:00:11.730 --> 00:00:15.090 Imagine you've got a tuple of currencies. 7 00:00:15.090 --> 00:00:17.240 Remember, this is how we can create a tuple. 8 00:00:17.240 --> 00:00:19.490 You can put the brackets in here or not 9 00:00:19.490 --> 00:00:21.230 if you don't want to. 10 00:00:21.230 --> 00:00:25.390 You can, just like you do currencies equal 0.8 comma 1.2 11 00:00:26.540 --> 00:00:30.653 you can do USD, EUR equal currencies. 12 00:00:31.490 --> 00:00:34.740 And what this means is we're going to get 13 00:00:34.740 --> 00:00:36.560 the first value of the tuple 14 00:00:36.560 --> 00:00:38.813 and put it into a new variable called USD. 15 00:00:39.760 --> 00:00:42.200 And we're gonna take the second value of the tuple 16 00:00:42.200 --> 00:00:44.750 and we're gonna put into a new variable called EUR. 17 00:00:46.520 --> 00:00:48.210 So you can imagine that 0.8 18 00:00:48.210 --> 00:00:50.890 is the United States dollar conversion rate 19 00:00:50.890 --> 00:00:53.130 and 1.2 is the Euro conversion rate 20 00:00:53.130 --> 00:00:55.220 from pounds of course. 21 00:00:55.220 --> 00:00:56.660 So this is called destructuring. 22 00:00:56.660 --> 00:01:00.420 It is taking a tuple, such as our currencies tuple, 23 00:01:00.420 --> 00:01:04.880 and turning it into multiple variables by this method. 24 00:01:04.880 --> 00:01:07.060 It's particularly interesting when you've got something 25 00:01:07.060 --> 00:01:11.580 like a list of tuples, say a list where the elements 26 00:01:11.580 --> 00:01:14.290 are all your friends names and ages. 27 00:01:14.290 --> 00:01:19.290 Rolf 25, Anne 37, Charlie 31, and Bob 22. 28 00:01:19.410 --> 00:01:21.657 Here we've got a list with four tuples 29 00:01:21.657 --> 00:01:24.890 and each tuple has a name and an age. 30 00:01:24.890 --> 00:01:27.570 Traditionally, we could do something like this. 31 00:01:27.570 --> 00:01:29.300 For friend in friends 32 00:01:29.300 --> 00:01:31.420 and we could print the friend. 33 00:01:31.420 --> 00:01:33.840 Now if we run this you'll see that Python 34 00:01:33.840 --> 00:01:35.443 prints out each of the tuples. 35 00:01:36.700 --> 00:01:40.220 However, using destructuring we can take directly 36 00:01:40.220 --> 00:01:43.650 instead of friend the name and the age. 37 00:01:43.650 --> 00:01:46.030 And what Python does is it says, 38 00:01:46.030 --> 00:01:51.030 okay each friend is a tuple so we can just take the name 39 00:01:51.210 --> 00:01:55.570 and the age as the first and second element of the tuple. 40 00:01:55.570 --> 00:01:59.453 And then you can print something like name and age. 41 00:02:00.520 --> 00:02:01.780 So this looks a little bit nicer 42 00:02:01.780 --> 00:02:02.940 but of course we can use them 43 00:02:02.940 --> 00:02:04.773 inside an f string if we prefer. 44 00:02:05.930 --> 00:02:07.513 And that will be even nicer. 45 00:02:09.030 --> 00:02:10.010 The reason I'm showing you this 46 00:02:10.010 --> 00:02:13.980 is because this stuff here, destructuring inside a for loop 47 00:02:13.980 --> 00:02:16.770 is something we're gonna use in future videos 48 00:02:16.770 --> 00:02:19.870 and it also makes your programmes easier to read. 49 00:02:19.870 --> 00:02:22.600 If you didn't do this you would have to do 50 00:02:22.600 --> 00:02:26.830 for friend in friends and then name, age equal friend. 51 00:02:26.830 --> 00:02:31.240 Or even worse, name equal friend zero 52 00:02:31.240 --> 00:02:33.970 and age equal friend one. 53 00:02:33.970 --> 00:02:35.760 These are all equivalent, 54 00:02:35.760 --> 00:02:40.620 I will just keep them here for you. 55 00:02:40.620 --> 00:02:43.550 So these are equivalent but of course 56 00:02:43.550 --> 00:02:47.210 this is much longer, this is slightly shorter 57 00:02:47.210 --> 00:02:50.030 and just putting that here is the shortest of them all 58 00:02:50.030 --> 00:02:53.110 and also the most readable once you know what it means. 59 00:02:53.110 --> 00:02:54.600 You do have to know what it means 60 00:02:54.600 --> 00:02:56.710 before this makes any sense of course 61 00:02:56.710 --> 00:02:58.510 but what it does, you'll see that, 62 00:02:58.510 --> 00:03:02.720 okay each element in friends has two values, name and age, 63 00:03:02.720 --> 00:03:06.750 and we can extract them from the tuples as we iterate. 64 00:03:06.750 --> 00:03:08.920 Again, that process is called destructuring. 65 00:03:08.920 --> 00:03:10.260 So I hope you've learned something. 66 00:03:10.260 --> 00:03:12.240 In the future we will make more use of this. 67 00:03:12.240 --> 00:03:14.993 Thanks for joining me and I'll see you in the next video.