Python 3, an Illustrated Tour Transcripts
Chapter: Language syntax
Lecture: Extended Unpacking
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
In this video we'll talk about extended iterable unpacking, it came about in Python 3, this is a nice feature. Let's dive into what that means
0:10
because extended iterable unpacking is quite a mouthful. Here's a simple motivation from the pep if I have an iterable here, range 5
0:19
and we know in Python 3 that range 5 is lazy, it only gives us values as we iterate over it unlike in Python 2 where it returns a list.
0:29
I might want to say, I want the first and last value but I don't care about the middle values or I want what's in between the first and last values,
0:37
this PEP allows us to do that, I can say a,* b and then c what this is going to do is it's going to say a gets the first value 0,
0:48
c is going to get the last value and *b gets what's ever left over, it unpacks those and note that this type here is a list,
0:58
it's going to put those into a list. A couple of notes here, this catch-all or starred expression is a list, it's not a tuple
1:06
if you're familiar with *args, when you're invoking functions and you look at the value of your args inside of your functions,
1:15
it's going to be a tuple, in this case, it's a list. You can only have one starred expression,
1:21
if you don't have these guys nested, so it's possible to nest them, and then in this little nested guy you can have a starred expression
1:27
but you can't have a *b *c and then d because it's ambiguous to Python and Python wants us to be explicit.
1:35
Another thing to note is that this deals with the left side of assignment. So this is unpacking and unpacking tuples or sequences
1:43
deals with the left side of assignment typically. Let's do a quick unpacking review, if you're not familiar with unpacking
1:51
or this feature that's in Python, that's pretty cool. This is all Python 2 compliant as well, so all this code works in Python 2 on this slide
1:57
if I've got a variable a and a variable b, you may or may not know that I can swap them easily simply by doing this,
2:05
a,b = b,a. What that's doing under the covers is it is saying on this side here, I'm actually making a tuple here
2:12
and I'm doing an unpacking operation here and I'm saying take the variable a and make it point to whatever the value of b was in this tuple
2:20
and make d point to whatever a is in this tuple. Now, it might seem confusing, how does it keep track of that, but under the covers what's happening is
2:29
this tuple has references to the values of b and a, not the actual variables. So it has references to those values
2:36
and then you're just pulling those out into new variables. Pretty cool. Okay, this next example here,
2:43
I have a list of names fred, george, luna and harry and I can do something like this where I say first, rest is equal to names 0 and names 1:
2:54
and this slice at the end here gives me names from index position 1 to the end.
3:01
When we look at first and rest we'll know that this first guy is a scalar. he's the fred guy, but the second guy here
3:09
is a list of those names or the sub guys there. This third example here in person, we have a tuple that has a nested tuple in it.
3:18
So it's got a name and age, some location and then it's got a tuple that presumably is dad and mom
3:25
so we can do something like this where we do an unpacking on the left hand side where we say name, age, location and then in parentheses here
3:32
dad mom is equal to person. What that will do is it will create a variable for each of those guys
3:37
and it will understand because dad and mom is in parentheses there it wants us to unpack that tuple of Arthur and Molly
3:46
and pull those into the variable dad and mom. Here below, you'll see that I have name and dad
3:51
and this is actually a tuple because it's got a comma between those and so we see that we have those values pulled out.
3:58
So that's unpacking, this works in Python 2 and 3. Here's another example of unpacking, this would work in Python 2
4:05
were it not for this fstring here, but I can do the same thing you may or may not know that if I'm in a for loop, a for loop creates variables,
4:12
and in this case, I'm unpacking those variables because I'm using enumerate, enumerate returns the index position and the items of enumeration.
4:20
In this case, the items of enumeration are tuples fred and age and george and the age, and so if I want to unpack fred and the age,
4:29
I need to put parentheses around those guys to pull them out as variables.
4:34
So inside of my for loop here, I have a variable called i that points to the index.
4:40
And in this case, I told it to start at 1 rather than the default of 0 and I also have a variable called name and age
4:46
and so I'm just going to print those out, I'm going to print out the index, the name, intense basis and the age here.
4:53
Okay, so this is the Python 3 stuff that's new. We've got some names again and I'm going to say first
4:59
and then I'm going to say *rest, so what that does here is it says I want first to unpack the value at the start
5:07
and star rest to take a list of everything else that's on the end and we'll see that it's putting that into a list here.
5:14
Again, this is Python 3 syntax, alternatively if I've got this nested tuple like I had before
5:19
with fred 20 England and then the nested tuple of Arthur and Molly I can say *ignore, I want to ignore or put everything at the front
5:28
in this ignore list here and then I'm going to unpack dad and mom there, that will give me a variable called dad and mom
5:36
and we'll actually create a variable called ignore that has the rest in it. I can also use multiple stars here if were nested, like I said here
5:43
so I'm going to say ignore everything at the start, except I'm going to have some thing at the end which is this tuple,
5:50
note that what's inside of this tuple here is two strings and in Python, strings are sliceable, and so I can say d first,*d
6:02
and that's going to take Arthur here and it's going to pull off the first thing of Arthur which is a, so d first, the value of d first is a,
6:11
d is going to be a list with r t h u r in it, m first is going to be the same, the first letter of Molly,
6:19
capital M and the *m will be a list with the rest of that in there. One thing that might bite you, you need to be aware of
6:26
you can't just have a star in front of a variable by itself. You're going to get an error that looks like this
6:31
that says syntax error, starred assignment, target must be in a list or a tuple. A fix for that is easy, you can just put a comma at the end there
6:40
and in this case, this is sort of a no op here, but if you wanted to do that you could or if you had something over here that's iterable
6:47
that isn't already a list, you could create a list easily by doing that. Of course, I would want to be a little bit more explicit
6:55
if I was doing this, I would say people is equal to list of names. To me that's more explicit and easier to read,
7:05
but you could do this if you wanted to. We've talked in this video about this new unpacking syntax
7:11
that allows you to put stars in the left hand side of an unpacked operation,
7:16
it's pretty cool and allows you to basically clob what's in a sequence into a list. 07:22 Hopefully this is useful to you and you can find places
7:26
where this will make your code easier to read and use.