Tuple Object Does Not Support Item Assignments

9. Tuples¶

9.1. Tuples are used for grouping data¶

We saw earlier that we could group together pairs of values by surrounding with parentheses. Recall this example:

>>> year_born=("Paris Hilton",1981)

This is an example of a data structure — a mechanism for grouping and organizing data to make it easier to use.

The pair is an example of a tuple. Generalizing this, a tuple can be used to group any number of items into a single compound value. Syntactically, a tuple is a comma-separated sequence of values. Although it is not necessary, it is conventional to enclose tuples in parentheses:

Tuples are useful for representing what other languages often call records — some related information that belongs together, like your student record. There is no description of what each of these fields means, but we can guess. A tuple lets us “chunk” together related information and use it as a single thing.

Tuples support the same sequence operations as strings. The index operator selects an element from a tuple.

>>> julia[2]1967

But if we try to use item assignment to modify one of the elements of the tuple, we get an error:

>>> julia[0]="X"TypeError: 'tuple' object does not support item assignment

So like strings, tuples are immutable. Once Python has created a tuple in memory, it cannot be changed.

Of course, even if we can’t modify the elements of a tuple, we can always make the variable reference a new tuple holding different information. To construct the new tuple, it is convenient that we can slice parts of the old tuple and join up the bits to make the new tuple. So if has a new recent film, we could change her variable to reference a new tuple that used some information from the old one:

To create a tuple with a single element (but you’re probably not likely to do that too often), we have to include the final comma, because without the final comma, Python treats the below as an integer in parentheses:

9.2. Tuple assignment¶

Python has a very powerful tuple assignment feature that allows a tuple of variables on the left of an assignment to be assigned values from a tuple on the right of the assignment. (We already saw this used for pairs, but it generalizes.)

(name,surname,b_year,movie,m_year,profession,b_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.

One way to think of tuple assignment is as tuple packing/unpacking.

In tuple packing, the values on the left are ‘packed’ together in a tuple:

>>> b=("Bob",19,"CS")# tuple packing

In tuple unpacking, the values in a tuple on the right are ‘unpacked’ into the variables/names on the right:

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 and :

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:

>>> (a,b,c,d)=(1,2,3)ValueError: need more than 3 values to unpack

9.3. Tuples as return values¶

Functions can always only return a single value, but by making that value a tuple, we can effectively group together as many values as we like, and return them together. This is very useful — we often want to know some batsman’s highest and lowest score, or we want to find the mean and the standard deviation, or we want to know the year, the month, and the day, or if we’re doing some some ecological modelling we may want to know the number of rabbits and the number of wolves on an island at a given time.

For example, we could write a function that returns both the area and the circumference of a circle of radius r:

Tuples items can themselves be other tuples. For example, we could improve the information about our movie stars to hold the full date of birth rather than just the year, and we could have a list of some of her movies and dates that they were made, and so on:

Notice in this case that the tuple has just five elements — but each of those in turn can be another tuple, a list, a string, or any other kind of Python value. This property is known as being heterogeneous, meaning that it can be composed of elements of different types.

9.5. Glossary¶

data structure

An organization of data for the purpose of making it easier to use.

immutable data value

A data value which cannot be modified. Assignments to elements or slices (sub-parts) of immutable values cause a runtime error.

mutable data value

A data value which can be modified. The types of all mutable values are compound types. Lists and dictionaries are mutable; strings and tuples are not.

tuple

An immutable data value that contains related elements. Tuples are used to group together related data, such as a person’s name, their age, and their gender.

tuple assignment

An assignment to all of the elements in a tuple using a single assignment statement. Tuple assignment occurs simultaneously rather than in sequence, making it useful for swapping values.

9.6. Exercises¶

We’ve said nothing in this chapter about whether you can pass tuples as arguments to a function. Construct a small Python example to test whether this is possible, and write up your findings.

Is a pair a generalization of a tuple, or is a tuple a generalization of a pair?

What happens is that creates and returns a new string value: the uppercased string . You can see this return value spit out on the screen (in shell environment only). The original string itself remains unchanged.

Contrast that with the behavior below. You have a list, and you reverse it by calling method:

First off, calling does not return anything: note that the shell quietly goes back to the prompt. Secondly, the original list itself has changed! That is because list methods such as change the caller object in place: they modify the calling object directly in memory. This property is what makes certain data types, including list, mutable.

Immutable objects, by contrast, do not change through a function. Below, calling , , , etc. all return a new string, and the original string remains unchanged. The only way to change the value of is through issuing a new assignment statement. Because returns a value, the variable gets newly assigned to it.

What if you tried something similar with a list, a mutable type? Here's what happens: because reverses the list in place and RETURNS NOTHING, the last variable assignment will assign to..., well, nothing. So, no, don't do this.

Now let's talk tuples. The whole point of tuples is that they are just like a list but immutable. Why is that necessary? Because lists grow and shrink, creating a list involves reserving a large block in the memory to accommodate its potential size. Tuples do not require such memory-side considerations. Therefore, for a list-like data that you know won't ever change, using a tuple is the smart thing to do. Below illustrates how tuples cannot be modified. Again, if you do need to change a tuple, you have to invoke a whole new assignment statement.