WEBVTT

1
00:00:00.520 --> 00:00:01.970
<v ->Hi guys, and welcome back!</v>

2
00:00:01.970 --> 00:00:03.230
In this video, we're going to learn

3
00:00:03.230 --> 00:00:05.680
about destructuring variables,

4
00:00:05.680 --> 00:00:08.060
which is something we looked at in the previous video,

5
00:00:08.060 --> 00:00:09.910
in the for-loop, but now we're going

6
00:00:09.910 --> 00:00:11.750
to explain how it works.

7
00:00:11.750 --> 00:00:13.790
Normally in Python when you assign a variable,

8
00:00:13.790 --> 00:00:16.963
you can do something like x equal five, say.

9
00:00:17.890 --> 00:00:22.890
But, you can also do x equal five comma 11.

10
00:00:22.990 --> 00:00:25.240
And, this, here is a tuple.

11
00:00:25.240 --> 00:00:27.720
We've learned about these in the past.

12
00:00:27.720 --> 00:00:31.980
But, what a lot of people don't know, is that the brackets

13
00:00:31.980 --> 00:00:34.920
are not necessary, the brackets are only necessary

14
00:00:34.920 --> 00:00:39.180
when Python might be confused as to whether you wanna create

15
00:00:39.180 --> 00:00:43.310
a tuple, or these are values in another collection.

16
00:00:43.310 --> 00:00:45.500
So if you have a tuple like this one,

17
00:00:45.500 --> 00:00:48.060
you can actually remove the brackets.

18
00:00:48.060 --> 00:00:52.580
But, if you wanted to create a list with a tuple inside it,

19
00:00:52.580 --> 00:00:54.320
then, you would need the brackets.

20
00:00:54.320 --> 00:00:56.180
Because, if you don't put the brackets in,

21
00:00:56.180 --> 00:00:58.400
you'll actually end with a list with two values,

22
00:00:58.400 --> 00:01:01.610
not a list with a tuple inside it.

23
00:01:01.610 --> 00:01:04.660
So, the brackets are only needed when you want to explicitly

24
00:01:04.660 --> 00:01:09.420
say, I want a tuple here, I don't want Python to treat these

25
00:01:09.420 --> 00:01:11.380
as two separate values.

26
00:01:11.380 --> 00:01:14.640
But most of the time, the brackets are not necessary,

27
00:01:14.640 --> 00:01:16.740
so you can leave it as this.

28
00:01:16.740 --> 00:01:19.390
Now, regarding destructuring variables,

29
00:01:19.390 --> 00:01:22.770
this tuple here can be destructured or decomposed,

30
00:01:22.770 --> 00:01:26.790
or split-out into two variables if you want.

31
00:01:26.790 --> 00:01:31.070
X, comma, y, and Python is smart enough to assign

32
00:01:31.070 --> 00:01:33.340
x to five and y to 11.

33
00:01:33.340 --> 00:01:35.880
It's just a shorthand, instead of defining

34
00:01:35.880 --> 00:01:37.720
two variables separately.

35
00:01:37.720 --> 00:01:40.400
The extra benefit that we get from being able

36
00:01:40.400 --> 00:01:44.270
to do something like this is that if you have another

37
00:01:44.270 --> 00:01:49.020
tuple, which is five and 11, you can then actually say

38
00:01:49.020 --> 00:01:52.570
something like this, x comma y equal t.

39
00:01:52.570 --> 00:01:55.000
And then, we can print x and y.

40
00:01:55.000 --> 00:01:58.540
So if we run this, you'll see that you get five and 11

41
00:01:58.540 --> 00:02:00.550
as the two separate variables.

42
00:02:00.550 --> 00:02:04.100
So, Python was smart enough to say, okay, t is a tuple,

43
00:02:04.100 --> 00:02:06.630
and you're asking me to assign this tuple to two variables,

44
00:02:06.630 --> 00:02:08.600
what I'm gonna do is I'm gonna split it out

45
00:02:08.600 --> 00:02:10.560
into it's components.

46
00:02:10.560 --> 00:02:13.713
So this is what destructuring really means.

47
00:02:16.270 --> 00:02:18.930
In the last video, we had some code like this one,

48
00:02:18.930 --> 00:02:23.360
where we have a dictionary of strings to integers,

49
00:02:23.360 --> 00:02:28.250
and then we iterated over student attendance dot items.

50
00:02:28.250 --> 00:02:33.250
Now, I'm going to print the list of that, which is going

51
00:02:33.560 --> 00:02:36.940
to take this stuff here, turn it into a list,

52
00:02:36.940 --> 00:02:38.230
and print it out.

53
00:02:38.230 --> 00:02:41.420
So, let me do that, and not print that,

54
00:02:41.420 --> 00:02:43.510
and show you what happens.

55
00:02:43.510 --> 00:02:48.050
Notice that what you get back is a list of tuples.

56
00:02:48.050 --> 00:02:51.570
So, if you iterate over a list of tuples,

57
00:02:51.570 --> 00:02:55.853
that means that, by doing something like this,

58
00:02:57.050 --> 00:03:01.280
what you are getting is three different tuples.

59
00:03:01.280 --> 00:03:04.250
Let me show you what happens if I say that,

60
00:03:04.250 --> 00:03:07.693
and then I print t there, and then I common that out.

61
00:03:09.560 --> 00:03:14.560
You can see that t is first a tuple of Rolf and 96,

62
00:03:15.110 --> 00:03:17.370
then a tuple of Bob and 80,

63
00:03:17.370 --> 00:03:19.463
and then a tuple of Anne and 100.

64
00:03:20.350 --> 00:03:23.940
So, instead of t, we can access its components

65
00:03:23.940 --> 00:03:27.550
because we can destructure this into two separate variables,

66
00:03:27.550 --> 00:03:29.650
and that's what we were doing earlier on,

67
00:03:29.650 --> 00:03:34.650
when we destructured the tuple into student, and attendance.

68
00:03:35.240 --> 00:03:39.660
Student would be Rolf, and attendance would be 96.

69
00:03:39.660 --> 00:03:41.670
And then the next time you go through the loop,

70
00:03:41.670 --> 00:03:43.210
you'd get the next tuple,

71
00:03:43.210 --> 00:03:46.270
and that would be destructured into its two components,

72
00:03:46.270 --> 00:03:47.690
again, and so on.

73
00:03:47.690 --> 00:03:51.620
So that, is why this works.

74
00:03:51.620 --> 00:03:55.010
Hopefully that makes sense, that is destructuring in use

75
00:03:55.010 --> 00:03:57.610
here in a for-loop, and you're gonna see it quite a lot

76
00:03:57.610 --> 00:03:59.150
when you do something like items,

77
00:03:59.150 --> 00:04:01.390
or when you use something like the enumerate function

78
00:04:01.390 --> 00:04:03.260
that I know we haven't spoke about yet,

79
00:04:03.260 --> 00:04:05.350
it is quite prevalent in Python.

80
00:04:05.350 --> 00:04:08.600
I'm going to link a blog post on destructuring variables

81
00:04:08.600 --> 00:04:10.600
that you can read as well if you'd like.

82
00:04:13.010 --> 00:04:14.750
Let's take a look at another example,

83
00:04:14.750 --> 00:04:18.510
here we have a list of people where instead of a dictionary,

84
00:04:18.510 --> 00:04:22.370
each person has a tuple with three values.

85
00:04:22.370 --> 00:04:25.570
Their name, their age, and their profession.

86
00:04:25.570 --> 00:04:28.230
Because we've got a tuple of three values,

87
00:04:28.230 --> 00:04:32.950
we can do for name, age, and profession in people,

88
00:04:32.950 --> 00:04:35.300
and that is going to, once again, iterate through each

89
00:04:35.300 --> 00:04:38.630
of the tuples, and for each one, destructure it into

90
00:04:38.630 --> 00:04:40.790
its three separate components.

91
00:04:40.790 --> 00:04:44.683
So here, we could print NA string describing this person,

92
00:04:46.440 --> 00:04:48.040
just like that.

93
00:04:48.040 --> 00:04:51.700
Notice that if any one of your tuples is missing a value,

94
00:04:51.700 --> 00:04:54.440
you're going to encounter some problems.

95
00:04:54.440 --> 00:04:57.420
It says, value error, not enough values to unpack.

96
00:04:57.420 --> 00:05:01.840
So you tried to access three values of this tuple,

97
00:05:01.840 --> 00:05:05.380
but there weren't three values, profession was missing,

98
00:05:05.380 --> 00:05:06.660
so you got this error.

99
00:05:06.660 --> 00:05:08.060
So this is something you have to be careful with,

100
00:05:08.060 --> 00:05:10.713
when you're doing this destructuring or unpacking.

101
00:05:12.170 --> 00:05:16.300
Notice that the alternative to doing this is to iterate over

102
00:05:16.300 --> 00:05:20.700
each tuple, and print out each thing, using indices.

103
00:05:20.700 --> 00:05:24.120
So, person zero would be the name, person one

104
00:05:24.120 --> 00:05:27.700
would be the age, and person two would be the profession,

105
00:05:27.700 --> 00:05:30.400
if you don't destructure the variable here.

106
00:05:30.400 --> 00:05:32.460
So, this is clearly much less readable,

107
00:05:32.460 --> 00:05:34.090
it's not as obvious what's going on,

108
00:05:34.090 --> 00:05:36.410
so destructuring these things does help your code

109
00:05:36.410 --> 00:05:37.313
look a bit nicer.

110
00:05:39.480 --> 00:05:41.260
Let's say you got a single tuple,

111
00:05:41.260 --> 00:05:44.610
and you want to extract a person's name and profession,

112
00:05:44.610 --> 00:05:47.013
but you don't care for the age.

113
00:05:47.950 --> 00:05:50.880
In Python, variables can contain and even start

114
00:05:50.880 --> 00:05:54.210
with an underscore, and that is frequently what's used as

115
00:05:54.210 --> 00:05:56.800
a variable when you want to ignore one.

116
00:05:56.800 --> 00:06:01.060
So, you can say name underscore profession equal person,

117
00:06:01.060 --> 00:06:02.760
and what this is going to do is it's going to create

118
00:06:02.760 --> 00:06:05.840
three variables, name, underscore, and profession,

119
00:06:05.840 --> 00:06:09.780
and Bob will become name, 42 will become underscore,

120
00:06:09.780 --> 00:06:11.720
and mechanic will become profession.

121
00:06:11.720 --> 00:06:16.720
But, the underscore on its own is so bad a variable name,

122
00:06:17.170 --> 00:06:18.900
that nobody would wanna use it,

123
00:06:18.900 --> 00:06:22.020
unless it was not meant to be used.

124
00:06:22.020 --> 00:06:25.120
So that is what the Python community has decided,

125
00:06:25.120 --> 00:06:27.550
that if you use an underscore variable on its own,

126
00:06:27.550 --> 00:06:30.003
that is because you don't care about this variable,

127
00:06:30.003 --> 00:06:32.150
this variable is meant to be ignored.

128
00:06:32.150 --> 00:06:35.740
So then, it's just as good to print name and profession out,

129
00:06:35.740 --> 00:06:38.663
if you wanted to just print the person's details out.

130
00:06:40.520 --> 00:06:43.210
Let's say you've got a list of five elements,

131
00:06:43.210 --> 00:06:44.720
one, two, three, four, and five,

132
00:06:44.720 --> 00:06:47.860
and you wanted to separate this into two lists.

133
00:06:47.860 --> 00:06:51.760
The first element, and all the other elements.

134
00:06:51.760 --> 00:06:56.240
Well, you can do head comma star tail,

135
00:06:56.240 --> 00:06:58.540
and that is the syntax for collecting.

136
00:06:58.540 --> 00:07:01.410
So, what you are doing, is you are collecting all of the

137
00:07:01.410 --> 00:07:04.760
destructured values into this variable.

138
00:07:04.760 --> 00:07:07.590
All of the values that don't match any of the other

139
00:07:07.590 --> 00:07:10.420
destructured values, which in this case you've got the head.

140
00:07:10.420 --> 00:07:13.290
So the first value in here will become the head,

141
00:07:13.290 --> 00:07:16.170
and then this syntax here will collect the other values,

142
00:07:16.170 --> 00:07:18.530
and will put them all into tail.

143
00:07:18.530 --> 00:07:21.010
So if you print head, and you print tail,

144
00:07:21.010 --> 00:07:24.540
then you'll see what happens is you get one, as the first

145
00:07:24.540 --> 00:07:28.460
value, and two, three, four, and five as the second value.

146
00:07:28.460 --> 00:07:31.760
Notice that you only need to do this when you want to put

147
00:07:31.760 --> 00:07:34.440
all the values into one place, you never need to use it

148
00:07:34.440 --> 00:07:36.983
again like that, you don't have to do that.

149
00:07:37.840 --> 00:07:39.870
Instead of that, you can move this of course,

150
00:07:39.870 --> 00:07:42.010
and put the star at the front if you like,

151
00:07:42.010 --> 00:07:43.280
and then this is going to collect

152
00:07:43.280 --> 00:07:44.950
as many values as possible,

153
00:07:44.950 --> 00:07:47.020
but leaving room for the tail,

154
00:07:47.020 --> 00:07:48.510
which is gonna be the last one.

155
00:07:48.510 --> 00:07:50.340
So, head will be one, two, three, four,

156
00:07:50.340 --> 00:07:53.840
and tail will be five, just like that.

157
00:07:53.840 --> 00:07:57.890
The asterisk can also be used here, we're not going

158
00:07:57.890 --> 00:07:59.190
to learn about that just now,

159
00:07:59.190 --> 00:08:00.960
we're going to learn about it in a few videos times,

160
00:08:00.960 --> 00:08:02.230
it's a little bit more advanced,

161
00:08:02.230 --> 00:08:05.870
so I wanna get into some of the other topics before

162
00:08:05.870 --> 00:08:07.620
we talk about that one.

163
00:08:07.620 --> 00:08:10.110
So, please do bear with me, and know that that will work

164
00:08:10.110 --> 00:08:12.470
if you do that, and it will print out something slightly

165
00:08:12.470 --> 00:08:15.280
different, and not printing the list anymore,

166
00:08:15.280 --> 00:08:17.040
now which brings you the separate values,

167
00:08:17.040 --> 00:08:19.640
and that is valid Python and you will use it

168
00:08:19.640 --> 00:08:21.680
quite a lot as you programme in Python,

169
00:08:21.680 --> 00:08:24.180
but again we're going to learn about it in a later video.

170
00:08:24.180 --> 00:08:27.000
So, please, bear with me, I hope this video's been useful,

171
00:08:27.000 --> 00:08:28.650
and I'll see you in the next one.

