Python 3, an Illustrated Tour Transcripts
Chapter: Language syntax
Lecture: Walk-through: Extended Unpacking
0:00 In this video, we're going to look at the unpack test assignment.
0:03 I'm using PyCharm so I'm going to expand my directory here
0:06 and click on unpack test.
0:10 We can see at the bottom here that when we execute this file,
0:13 it will try and run pytest against it
0:15 and we can see that PyCharm is complaining about various names that are missing.
0:19 Let's read the assignment and see what we need to do.
0:21 It says given the tuple person, which is defined right here,
0:25 unpack the values into name, age and country.
0:28 So person is a tuple with a string which appears to be the name
0:33 a number which looks like it's the age
0:36 and a string which appears to be a country.
0:39 One way to do this would be to say name is equal to person 0, index position 0
0:44 and age is equal to person 1, and country is equal to person 2.
0:52 Let's run and make sure it works,
0:54 note that because I just opened this and I had previously run vm.test
0:57 If I say run here, it's not going to run this file
1:00 a couple things I can do, I can right click here and say run unpack test.
1:04 I can also run it from over here by right clicking on the file
1:08 and running it over there.
1:10 There's also a command line shortcut, it's control shift F10.
1:13 Let's try and do that and see what happens when we run this.
1:15 Okay, so it appears that I got to line 21.
1:22 There is one failure pytest ran and so it appeared
1:26 that we made this first part work.
1:28 Now, we really didn't do an unpack operation
1:31 what an unpack operation does is it says
1:33 I know that a sequence contains a certain amount of items
1:36 and I want to in a single assignment
1:39 with commas in between the variable names pull those out.
1:42 So this is how we would do the unpack here.
1:45 We would say name, age, country is equal to person,
1:51 let's just run that again to make sure it still works.
1:53 Okay, we still get the name around the next part.
1:56 So that's how we do basic unpacking and this works in Python 2 & 3.
1:59 Let's look at extended unpacking,
2:01 extended unpacking remember is where we put a star in front of a variable.
2:05 So it says use unpacking to get the first letter of the name, store the result in first.
2:10 So one way to do this is just to say name is a string
2:14 and let's pull off the first character, first is equal to that.
2:16 If we want to do this using unpacking though or extended unpacking,
2:20 one way would be to say first, second, third
2:27 but we don't know how many, perhaps we don't know,
2:30 in this case we do know how many characters there are
2:32 but it'd be kind of annoying to type all those out
2:34 and if the length is dynamic, it's not going to work.
2:38 What Python allows us to do is put a little star in front of the next variable
2:41 and just say something like rest
2:43 and what's going to happen here is first will be the first item of the sequence
2:47 and rest will be a list containing the rest of the items.
2:50 I'm going to use a feature of PyCharm here to just put a breakpoint in here
2:57 and now I'm going to click the bug up here and let's inspect what's going on.
3:01 I want to look at rest and it looks like rest is a list and it has a length of 0.
3:13 Let's see what name is or first, first is c.
3:18 Okay, so it looked like it didn't pull out rest
3:23 or PyCharm thinks that it's an empty list, which is interesting.
3:27 Oh, that's because I have an error.
3:30 I'm going to hit play and just fix my error here.
3:33 The problem is because I gave it a list of length 1 because I left the 0 up here.
3:38 Let's get rid of that and let's debug it again.
3:43 Okay, in this case now rest is a list
3:45 and it has each of the characters in there.
3:48 So when you put a star in front of it, that's just going to make a list
3:51 and put the remaining items in there.
3:53 And because a list can be variable length in Python
3:56 this will support arbitrary length of names there.
4:00 Okay the final assignment here is
4:05 unpack the characters from name into a list called letters.
4:10 So one way to do this, you could say letters is equal to list of name
4:14 and that should work.
4:20 It appears to work, but we want to use unpacking to this,
4:24 in particular, we want to use extended unpacking.
4:26 So let's try and do it extended unpacking has remember that little star in front of it
4:31 *letters is equal to name.
4:35 Let's run that and see what happens.
4:39 I get a syntax error starred assignment target must be in a list or a tuple.
4:44 So what that means is that I can't have a standalone variable with a star in front of it,
4:49 Python 3 requires that I put that comma right there
4:52 to indicate that we're going to unpack this into a list.
4:57 Let's try it now and it looks like we get the right thing.
5:00 So hopefully you've learned a little bit about unpacking
5:06 and extended unpacking in Python 3.
5:08 This is a great way to pull out either the first or the last of a sequence.
5:12 Remember that when you use the extended unpacking
5:15 with a star in front of something,
5:17 there has to be at least more than one variable
5:20 or you have to put a comma following it,
5:22 also recall that when you use extended unpacking
5:24 with a star in front of the variable name, that variable will be turned into a list.