I l@ve RuBoard |
The last collection type in our survey is the Python tuple. Tuples construct simple groups of objects. They work exactly like lists, except that tuples can't be changed in place (they're immutable) and are usually written as a series of items in parentheses, not square brackets. Tuples share most of their properties with lists. They are:
Like strings and lists, tuples are an ordered collection of objects; like lists, they can embed any kind of object.
Like strings and lists, items in a tuple are accessed by offset (not key); they support all the offset-base access operations we've already seen, such as indexing and slicing.
Like strings, tuples are immutable; they don't support any of the in-place change operations we saw applied to lists. Like strings and lists, tuples are sequences; they support many of the same operations.
Because tuples are immutable, they can't grow or shrink without making a new tuple; on the other hand, tuples can hold other compound objects (e.g., lists, dictionaries, other tuples) and so support nesting.
Like lists, tuples are best thought of as object reference arrays; tuples store access points to other objects (references), and indexing a tuple is relatively quick.
Table 2.9 highlights common tuple operations. Tuples are written as a series of objects (really, expressions), separated by commas, and enclosed in parentheses. An empty tuple is just a parentheses pair with nothing inside.
Operation |
Interpretation |
---|---|
() |
An empty tuple |
t1 = (0,) |
A one-item tuple (not an expression) |
t2 = (0, 1, 2, 3) |
A four-item tuple |
t2 = 0, 1, 2, 3 |
Another four-item tuple (same as prior line) |
t3 = ('abc', ('def', 'ghi')) |
Nested tuples |
t1[i], t3[i][j] t1[i:j], len(t1) |
Index, length |
t1 + t2 t2 * 3 |
Concatenate, repeat |
for x in t2, 3 in t2 |
Iteration, membership |
The second and fourth entries in Table 2.9 merit a bit more explanation. Because parentheses can also enclose expressions (see Section 2.3), you need to do something special to tell Python when a single object in parentheses is a tuple object and not a simple expression. If you really want a single-item tuple, simply add a trailing comma after the single item and before the closing parenthesis.
As a special case, Python also allows us to omit the opening and closing parentheses for a tuple, in contexts where it isn't syntactically ambiguous to do so. For instance, in the fourth line of the table, we simply listed four items, separated by commas; in the context of an assignment statement, Python recognizes this as a tuple, even though we didn't add parentheses. For beginners, the best advice here is that it's probably easier to use parentheses than it is to figure out when they're optional.
Apart from constant syntax differences, tuple operations (the last three rows in the table) are identical to strings and lists, so we won't show examples here. The only differences worth noting are that the +, *, and slicing operations return new tuples when applied to tuples, and tuples don't provide the methods we saw for lists and dictionaries; generally speaking, only mutable objects export callable methods in Python.
This seems to be the first question that always comes up when teaching beginners about tuples: why do we need tuples if we have lists? Some of it may be historic. But the best answer seems to be that the immutability of tuples provides some integrity; you can be sure a tuple won't be changed through another reference elsewhere in a program. There's no such guarantee for lists, as we'll discover in a moment. Some built-in operations also require tuples, not lists; for instance, argument lists are constructed as tuples, when calling functions dynamically with built-ins such as apply (of course, we haven't met apply yet, so you'll have to take our word for it for now). As a rule of thumb, lists are the tool of choice for ordered collections you expect to change; tuples handle the other cases.
I l@ve RuBoard |