Python 3, an Illustrated Tour Transcripts
Chapter: Language syntax
Lecture: Walk-through: Extended Unpacking

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


Talk Python's Mastodon Michael Kennedy's Mastodon