Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Packing and Unpacking Tuples

00:00 In Python, we have the concept of packing and unpacking iterables, and this includes tuples. So what is packing? Packing is storing multiple values into a single variable.

00:11 In this example, this line of code creates a variable called point and within it stores the tuple of integer values 7, 14, and 21.

00:20 This should look familiar because this is just another way to refer to creating a tuple literal. Much more exciting is unpacking. Unpacking uses multiple variables to store the values of a single iterable.

00:33 This line of code unpacks the values of the variable point into the three variables on the left hand side of the assignment. After running this code, the variable x holds the value 7.

00:47 The variable y holds the value 14, and the variable z holds the value 21. Ready to write some code? Head to the REPL.

00:57 You can start by recreating the code from the previous slide. point is a tuple with three integer values, 7, 14, and 21.

01:06 You can imagine this being a point in three-dimensional space. As is convention, you can use x, y, and z to represent each individual value.

01:16 On the left hand side, you will have a comma-separated sequence of variable names, and on the right-hand side of the assignment, you will have an iterable with the same number of elements as there are variables on the left-hand side.

01:28 Now x holds the value of 7, y holds the value of 14, and z holds the value of 21.

01:36 The order of variable names matches the order of values in the original tuple. You can even cut out the middleman and do something called parallel assignment where you use a comma-separated sequence on both sides of the assignment.

01:50 Like this, you can directly assign three different values to x, y, and z. But I will note this should really only be used for closely-related variables.

02:00 An important warning is that the number of variables should match the number of elements you wish to unpack. If not, you will get an error. For example, if you try to unpack the three elements of point into two variables, for instance x, y = point, you’ll get a value error because there are more values than available variable names.

02:22 Now, one of the best use cases for unpacking is when a tuple is returned from a function and you’d like to store each element in its own variable. For example, you can apply this to the built-in divmod() function.

02:34 Don’t know about divmod()? Well, you’re about to find out. divmod() takes two numbers as arguments and returns a tuple of numbers consisting of their quotient and remainder when using integer division.

02:47 Now you can unpack quotient and remainder from calling divmod() with two numbers. Here I’ll be using 14 and 4.

03:00 Take a look at the values in quotient and remainder. Because 14 can only be evenly divided by 4 three times, I get a quotient of 3, and because there will still be a 2 leftover, I get a remainder of 2.

03:16 A huge advantage of this technique is it makes your code much more readable.

03:23 Now, I’ve said you need to have the same number of variables on the left-hand side as there are elements in the iterable on the right-hand side, but that’s not entirely true.

03:30 You can get around that by using the packing and unpacking operator represented by an asterisk, or a star. First, create a tuple of numbers one to five.

03:43 Now say you want to extract the last number as its own variable, but the rest can stay in some kind of container. You can provide two variables on the left-hand side and use the star operator on the first one.

03:55 Do this and all values other than the last will be packed into the first variable. *head, last = numbers.

04:05 Now examine the new variables head and last. head is now a list of the first four numbers, and last is the integer five, which was the last number in the tuple.

04:15 However you arrange the variables, Python will use the starred variable to store the leftover elements in the iterable. For example, if you want to extract the first and last numbers, you can pack the rest in a variable called middle. first, * middle, last = numbers.

04:35 Take a look at first, middle, and last.

04:40 Now first holds the first value in the tuple, which is the integer 1. last holds the last value, the integer 5, and middle holds the values in between.

04:50 And if you don’t have need of the leftover values, you can actually use the convention of *_ indicating the variable won’t be used.

05:04 You might notice that this has the same effect as retrieving the first element by index, and generally that would be the more natural approach, but this is a common pattern you’ll encounter in other people’s code, often when dealing with function returns.

05:17 Using an underscore as a variable name is a common practice in Python to show that you don’t have any plans to use the values contained in that variable.

05:26 Another thing to note, when using the star operator, whatever the iterable on the right-hand side, the star variable will still hold a list and not a tuple.

05:36 You can also use the star operator to combine tuples. Want to see how? Join me in the next lesson, which is all about combining and extending tuples.

Become a Member to join the conversation.