Python Fundamentals

Unpacking Tuples

Python Fundamentals

Check out a free preview of the full Python Fundamentals course

The "Unpacking Tuples" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates how to assign variables to a tuple's values for use outside of the tuple.


Transcript from the "Unpacking Tuples" Lesson

>> Nina Zakharenko: Tuples are also a great way to quickly consolidate information because of something called tuple unpacking. So looking back at our student, we have a name, an age, a subject, and a GPA.
>> Nina Zakharenko: We can unpack this by declaring variable names on the left side of the expression.

And matching them up with the number of items in the tuple. And separating them with a comma. Second say, name, comma, age, comma, subject, comma, gpa, equals student. It looks a little weird, right?
>> Nina Zakharenko: But now, all of a sudden, my variable name is filled in, age is filled in, subject filled in, gpa filled in.

Super easy, right? If this was a row in a spreadsheet, I could just pull out the values super quickly.
>> Nina Zakharenko: As we're first starting out, and this is not a syntax rule in Python, there's a way of doing it. But generally, we want the number of variables that we declare to match up to the number of items in the tuple.

If you don't believe, you will see an error. Let me double check that. Right, yeah, you'll see a ValueError, too many values to unpack.
>> Nina Zakharenko: If we don't care about saving one of the values as a variable we can use the underscore. So let's say, I just want a name, age and the subject.

And I don't care about Marcy's GPA, I can use the shorthand underscore. And if I sign that to student my name, age and subject will be set. But this is just, it means throw away this value, I don't really care. I know this is super weird, but you'll probably see it in Python code out in the wild, and then you'll know what it means.

>> Nina Zakharenko: You can return tuples from functions and use on packing. Let's see that very quickly. And before moving on, so I told you that parenthesis mean the tuple but that's not entirely true right? It's actually the comma. I can also declare my tuple like this, without the parentheses, right?

Cuz it's the comma that makes a difference. Let's say, I had a method to get back an HTTPS status code.
>> Nina Zakharenko: I can return two values, 200, which is the status code, and OK, which is the name. What type will this return? It will return tuple, right? Why, because of the commas, there we go.

Now, I can use tuple unpacking. If I know that this function returns a tuple, I can use tuple unpacking to quickly get the values out as named variables. So I can say that the code and the name = http_status_code. And that code will be 200, name will be okay.

So, think tuples worth their while now?
>> Class: Yeah.
>> Nina Zakharenko: Yeah.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now