Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Unpacking

Reading a tuple by index works, but point[0] doesn't say what's in it. Instead, you can give each element a variable of its own, all in one line:

point = (3, 8)
x, y = point
print(x)
print(y)
3
8

On the left of the = are two names, separated by a comma. Python puts the first element of the tuple into the first name and the second element into the second. This is called unpacking.

You have used it before. In the lesson on dictionaries, for country, capital in capitals.items(): unpacked each pair of the dictionary into two loop variables. And without .items(), the loop got only the keys: Python tried to unpack a word such as "Czechia" into the two variables, and the program ended with ValueError: too many values to unpack (expected 2).

The number of names has to match the number of elements. When it doesn't, you get a ValueError that says how many values Python expected and how many it got:

x, y = (3, 8, 1)
Traceback (most recent call last):
  File "<exec>", line 1, in <module>
ValueError: too many values to unpack (expected 2, got 3)

With too few elements, x, y, z = (3, 8) ends with ValueError: not enough values to unpack (expected 3, got 2).

Unpacking works with lists too, such as the list that .split() gives. A result saved as "Anna:12" splits into a name and a score:

name, score = "Anna:12".split(":")
print(name)
print(int(score) + 1)
Anna
13

The score is still text, "12", so you need int() to calculate with it. Without it, score >= 10 would end with TypeError: '>=' not supported between instances of 'str' and 'int': text and a number can't be compared.

One more trick: unpacking can swap the values of two variables in a single line.

first = "Tom"
second = "Anna"
first, second = second, first
print(first, second)
Anna Tom

On the right, second, first makes a tuple of the two values (it's the commas that make a tuple, even without parentheses), and Python then unpacks it into first and second.

Instructions

  1. 1

    Split result at the colon and unpack the two parts into the variables name and score, all in one line. Then print them: print(name, score) shows Anna 12.

  2. 2

    Players with at least 10 points go on to the final. Below, print Anna goes to the final. if the score is 10 or more, and Anna is out. if it isn't, with the name taken from name. Keep the line result = "Anna:12": the exercise also tries other results.

Back

2/11

  1. 1. Tuples
  2. 2. Unpacking
  3. 3. Quiz: Unpacking
  4. 4. Returning Several Values
  5. 5. A List of Tuples
  6. 6. Numbering with enumerate()
  7. 7. Sets
  8. 8. Adding to a Set
  9. 9. Comparing Sets
  10. 10. Quiz: Sets
  11. 11. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.