13.4. Tuple Assignment with unpacking

Python has a very powerful tuple assignment feature that allows a tuple of variable names on the left of an assignment statement to be assigned values from a tuple on the right of the assignment. Another way to think of this is that the tuple of values is unpacked into the variable names.

This does the equivalent of seven assignment statements, all on one easy line. One requirement is that the number of variables on the left must match the number of elements in the tuple.

Once in a while, it is useful to swap the values of two variables. With conventional assignment statements, we have to use a temporary variable. For example, to swap a and b:

Tuple assignment solves this problem neatly:

The left side is a tuple of variables; the right side is a tuple of values. Each value is assigned to its respective variable. All the expressions on the right side are evaluated before any of the assignments. This feature makes tuple assignment quite versatile.

Naturally, the number of variables on the left and the number of values on the right have to be the same.

Earlier we were demonstrating how to use tuples as return values when calculating the area and circumference of a circle. Here we can unpack the return values after calling the function.

Python even provides a way to pass a single tuple to a function and have it be unpacked for assignment to the named parameters.

If you run this, you will be get an error caused by line 7, where it says that the function add is expecting two parameters, but you’re only passing one parameter (a tuple). In line 6 you’ll see that the tuple is unpacked and 5 is bound to x, 4 to y.

Don’t worry about mastering this idea yet. But later in the course, if you come across some code that someone else has written that uses the * notation inside a parameter list, come back and look at this again.

Note

Unpacking into multiple variable names also works with lists, or any other sequence type, as long as there is exactly one value for each variable. For example, you can write x, y = [3, 4].

13.5. Unpacking Into Iterator Variables

Multiple assignment with unpacking is particularly useful when you iterate through a list of tuples or lists.

For example, a dictionary consists of key-value pairs. When you call the items() method on a dictionary, you get back a sequence of key-value pairs. Each of those pairs is a two-item tuple. (More generally, we refer to any two-item tuple as a pair). You can iterate over the key-value pairs.

Each time line 4 is executed, p will refer to one key-value pair from d. A pair is just a tuple, so p[0] refers to the key and p[1] refers to the value.

That code is easier to read if we unpack the key-value pairs into two variable names.

More generally, if you have a list of tuples that each has more than two items, and you iterate through them with a for loop pulling out information from the tuples, the code will be far more readable if you unpack them into separate variable names right after the word for.

Check your Understanding

    tuples-4-1: If you want a function to return two values, contained in variables x and y, which of the following methods will work?
  • Make the last two lines of the function be "return x" and "return y"
  • As soon as the first return statement is executed, the function exits, so the second one will never be executed; only x will be returned
  • Include the statement "return [x, y]"
  • return [x,y] is not the preferred method because it returns x and y in a list and you would have to manually unpack the values. But it is workable.
  • Include the statement "return (x, y)"
  • return (x, y) returns a tuple.
  • Include the statement "return x, y"
  • return x, y causes the two values to be packed into a tuple.
  • It's not possible to return two values; make two functions that each compute one value.
  • It is possible, and frequently useful, to have one function compute multiple values.

    tuples-4-2: Consider the following alternative way to swap the values of variables x and y. What’s wrong with it?

    # assume x and y already have values assigned to them
    y = x
    x = y
    
  • You can't use different variable names on the left and right side of an assignment statement.
  • Sure you can; you can use any variable on the right-hand side that already has a value.
  • At the end, x still has it's original value instead of y's original value.
  • Once you assign x's value to y, y's original value is gone.
  • Actually, it works just fine!
  • Once you assign x's value to y, y's original value is gone.
3. With only one line of code, assign the variables water, fire, electric, and grass to the values “Squirtle”, “Charmander”, “Pikachu”, and “Bulbasaur”
4. With only one line of code, assign four variables, v1, v2, v3, and v4, to the following four values: 1, 2, 3, 4.
1. If you remember, the .items() dictionary method produces a sequence of tuples. Keeping this in mind, we have provided you a dictionary called pokemon. For every key value pair, append the key to the list p_names, and append the value to the list p_number. Do not use the .keys() or .values() methods.
2. The .items() method produces a sequence of key-value pair tuples. With this in mind, write code to create a list of keys from the dictionary track_medal_counts and assign the list to the variable name track_events. Do NOT use the .keys() method.
Next Section - 13.6. Glossary