Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Unpacking3:04 with Ashley Boucher
Now that you understand packing, learn about its opposite: unpacking!
All right, that you're familiar with packing, let's talk about unpacking. 0:00 Unpacking is basically the opposite of packing. 0:03 It's exploding a Python sequence into individual variables. 0:06 This is also sometimes called multiple assignment. 0:10 You'll see this often in function returns, but 0:13 that's definitely not the only place where this comes in handy. 0:15 The same is true for packing as well. 0:18 Both packing and unpacking are extremely useful Python tools that you can take 0:19 advantage of throughout your codebase. 0:23 Here we have a function called unpacker. 0:25 Unpacker doesn't do a whole lot. 0:28 It simply returns a tuple with three values in it. 0:29 One, two, and three. 0:32 Unpacking, however, 0:34 allows us to assign each of those values inside the tuple to its own variable. 0:35 That's gonna look like this. 0:40 Let's discuss what's happening here. 0:50 On the right side of this variable assignment, I'm calling the function, 0:51 unpacker. 0:55 Unpacker returns a tuple containing the values 1, 2, and 3. 0:57 Because the left side of our variable assignment has more than one variable 1:02 name, Python understands that we're trying to unpack this tuple. 1:05 Just like passing multiple arguments to multiple parameters is positional, 1:09 so is unpacking a sequence. 1:13 In this example, the first element in the tuple 1 will be assigned to var1. 1:14 The second, 2, will be assigned to var2. 1:22 And finally, 3 will be assigned to var3. 1:24 I'm gonna print out each of these variables so you can see. 1:28 Now I'll save this and run it. 1:41 Awesome, each element in the tuple was assigned to its own variable and 1:48 printed to its own line. 1:52 Notice how in the variable assignment, 1:54 I matched the number of variables exactly to the length of the tuple. 1:56 Unpacking is strict and requires exactness. 1:59 Open up the attached workspace. 2:03 Inside you'll see what's up here on screen. 2:05 Pause the video and try to change the code to see what happens. 2:07 What's the result of removing a variable name on the left side? 2:10 What about adding one? 2:13 What about changing the two bullets returned from the function? 2:15 After you're done playing around, unpause the video and we'll keep going. 2:18 Welcome back. 2:22 We've just seen how unpacking works with tuples. 2:24 But it also works with any Python sequence, even strings. 2:26 Let's rewrite the unpacker function a little bit so it returns a string. 2:30 Now, keeping everything else the same, let's run the program and 2:40 see what it prints out. 2:42 Let me clear this so there's a little more room. 2:43 Cool, each letter of the Python sequence hey, which is just a string, 2:51 has been unpacked into its own variable. 2:54 This also works with lists. 2:57 If you can loop over it, you can unpack it. 2:58 Let's more forward and look at a practical example. 3:01
You need to sign up for Treehouse in order to download course files.Sign up