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

Unlock This Lesson

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

Unlock This Lesson

Unpack Tuples

00:00 In this lesson, you’ll learn about unpacking tuples. Unpacking is an interesting concept in Python that allows you to do some fun things. Let’s backtrack a tiny bit and go back to how you can create a tuple literal, and you can do it by listing values separated by commas.

00:15 And generally, you would do this with parentheses, but the parentheses are optional. You can also do it without them. And in both cases, that creates a tuple literal.

00:25 I want to show you this without the parentheses because it gives some sort of a nice balance to the syntax that you use for tuple unpacking, and that is like so.

00:35 You can assign elements of a tuple to multiple variables by unpacking the tuple, and that’s what’s called tuple unpacking. So with the rgb_lime_green that I’ve defined in the previous slide, you can assign each individual element of the tuple to variables with the syntax of saying red, green, blue = and then the tuple literal on the other side.

01:01 Let’s try that out in IDLE.

01:04 I’m going to define rgb_lime_green. So that’s a color code for lime green, and it has the values of red 50, green 205, and blue 50.

01:21 I could define this without the parentheses as well, but I generally tend to always write the parentheses when I define a tuple. Okay, and now I can unpack this tuple, red, green, blue = rgb_lime_green, and now I can see that red points to 50, green to 205, and blue to 50 again. Now you don’t have to go the path across this intermediate variable here that I defined.

01:49 So I could also say black is RGB 0, 0, 0. So I could also say red, green blue values of black are going to be 0, 0, 0,

02:05 and now I’ve overwritten the variables above. And now the separate values all point to 0 in this case. Whether you use parentheses here or not, or whether you use an intermediate variable or not, what you do is on the right side of the assignment statement, you have a tuple literal, and on the left side you have the equal amount of variables that you have elements in the tuple, and then Python’s tuple unpacking assigns the first element to the first variable, the second element to the second variable, and the third element to the third variable, et cetera if there are more elements.

02:43 So that’s a feature that can sometimes be neat and useful. What you’ve just seen before is you can also use tuple unpacking to assign multiple variables in one line.

02:52 For example, here, going back to the database record that we’ve looked at before, you could assign a couple of variables like that in one go.

03:05 So I could say employee_id, name and role is going to be 1 will be the employee_id, name will be Adisa, and role software engineer.

03:15 So you can do this all in one line. Now you can see that they all point to what you’d expect them to point to.

03:24 Again, what you do here is nothing other than defining a tuple literal on the right side and then unpacking it, assigning each element of the tuple to the respective variable on the left side.

03:41 It’s a fun trick, but you shouldn’t overdo it because this only makes sense if it makes your code more readable. An example here of a not well readable way of doing this, which is just too many different variables, too many unrelated values, and that’s confusing and it would make more sense to structure this differently.

03:58 What could be readable is if both of the values are quite related to each other—for example, in a geographical location, and you have latitude and longitude, then you could assign them in this way in just one line instead of assigning them on two separate lines.

04:14 So this is tuple unpacking. What it allows you to do is assigning multiple variables in one line.

04:21 One more thing to remember about tuple unpacking is that for it to work, the number of variables must match the number of values.

04:30 Let’s try that out. If you would try to do something like a, b, c and not provide enough values—so here I’m providing three variables, a, b, c, but only two values, 1 and 2—then Python is going to throw a ValueError that there aren’t enough values to unpack.

04:47 And here you can see the word that Python is trying to unpack the tuple literal 1, 2 that you have here on the right side of the assignment statement, and it expected three, but got only two, and the same goes the other way.

04:59 So if you only have two variables, but you give it three values, then Python’s also going to complain with a ValueError, and it tells you that there’s too many values to unpack, and it only expected two, because there’s only two variables where to put the values into.

05:19 a, b, c = 1, 2, 3 works because the amount of variables that you provide for Python and the amount of values that you provide is equal. If it isn’t, you’ll get an error.

Become a Member to join the conversation.