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.
(name, surname, birth_year, movie, movie_year, profession, birth_place) = julia
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
:
temp = a
a = b
b = temp
Tuple assignment solves this problem neatly:
(a, b) = (b, a)
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.
(a, b, c, d) = (1, 2, 3) # ValueError: need more than 3 values to unpack
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.
-
rec-5-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.
- 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.
rec-5-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