11.3. Tuple Assignment¶
One of the unique syntactic features of Python is the ability to have a tuple on the left side of an assignment statement. This allows you to assign more than one variable at a time when the left side is a sequence.
In this example we have a two-element list (which is a sequence) and
assign the first and second elements of the sequence to the variables
x
and y
in a single statement.
This isn’t magic! Python roughly translates the tuple assignment syntax to the following:
It’s worth noting that Python does not translate the syntax literally. For example, if you try this with a dictionary, it won’t work as you might expect.
Stylistically, when we use a tuple on the left side of the assignment statement, we omit the parentheses, but the following is an equally valid syntax:
- ['Harden']
- Incorrect! The values in random_list are strings, not lists. Try again.
- 'James'
- Incorrect! x is listed before y in the tuple on the left side of the assignment statement, so the first value in random_list should be assigned to x. Try again.
- 'Harden'
- Correct! This properly assigns the first element of the list to 'x'.
11-9-4: What is associated with the variable ‘x’ once the following code is run?
random_list = ['Harden', 'James']
(x, y) = random_list
A particularly clever application of tuple assignment allows us to swap the values of two variables in a single statement:
>>> a, b = b, a
Both sides of this statement are tuples, but Python interprets the left side to be a tuple of
variables and the right side to be a tuple of expressions. All of
the expressions on the right side are evaluated before any of the
assignments. This means that the values of b
and a
on the right side are evaluated,
then a
and b
on the left side take on their values.
The number of variables on the left and the number of values on the right must be the same:
>>> a, b = 1, 2, 3
ValueError: too many values to unpack
Write code to swap the values of tuple t.
More generally, the right side can be any kind of sequence (string, list, or tuple). For example, to split an email address into a username and a domain, you could write:
>>> addr = 'monty@python.org'
>>> uname, domain = addr.split('@')
The return value from split()
is a list with two elements;
the first element is assigned to uname
, the second to
domain
.
>>> print(uname)
monty
>>> print(domain)
python.org
- hotmail.com
- Correct! The split() method splits the email address at '@'.
- @hotmail.com
- Incorrect! The split() method doesn't include its parameter in any of the elements of its returned list. Try again.
- ['hotmail.com']
- Incorrect! The split() method returns a list of strings, not a list of lists. Try again.
11-9-6: What is associated with the variable ‘domain’ once the following code is run?
email = yeezy@hotmail.com
uname, domain = email.split('@')