Tuples
Tuples construct simple groups of objects. They work same as lists do, except that tuples cannot be changed in-place (they are immutable) and are normally written as a series of items in parentheses (), not square brackets []. Although they do not support as many methods, tuples share most of its properties with lists. Here’s a quick look at the basics. Tuples are:
Ordered collections of arbitrary objects
Like strings and lists, tuples are positionally ordered collections of objects (i.e., they maintain a left-to- right order among their contents); like lists, they can embed any kind of object.
Accessed by offset
Similar to strings and lists, items in a tuple are accessed by offset (not by key); they support all the offset-based access operations, like indexing and slicing.
Of the category “immutable sequence”
Like strings and lists, tuples are in sequence; they support a lot of the same operations. However, tuples are immutable; they do not support any of the in-place change operations put in to lists
Fixed-length, heterogeneous, and arbitrarily nestable
Because tuples are immutable, you can't change the size of a tuple without creating a copy. Also, tuples can hold any type of object, including other compound objects, and so support arbitrary nesting.
Arrays of object references
Similar to lists, tuples are best thought of as object reference arrays; tuples store references to other objects, and indexing a tuple is relatively fast.
the table given below highlights common tuple operations. A tuple is written as a series of objects, separated by commas and generally enclosed in parentheses (tuple). An empty tuple is just a parentheses pair with nothing inside it().
Tuples in Action
Notice in the above table, that tuples don't have all the methods that lists have. They do, however, support the general sequence operations that we saw for both lists and strings:
The second and fourth entries in the given table no. 1 above, need a bit more explanation. Because parentheses can also enclose expressions , we need to do something unique to tell Python when a single object in parentheses is a tuple object and not a simple expression. If you want a single-item tuple, simply add a trailing comma after the single item:
As a special case, Python allows you to omit the opening & closing parentheses for a tuple in contexts where it is not syntactically ambiguous to do so. For instance, the fourth line of Table 1 given above simply lists four items separated by commas. In the context of an assignment statement, Python knows this as a tuple, yet it doesn’t have parentheses.
Now, someone will tell you to always use parentheses in your tuples, and other will tell you to never use parentheses in tuples. The only necessary places where the parentheses have to be are when a tuple is passed as a literal in a function call.
Conversions, methods, and immutability
Apart from literal syntax differences, tuple operations are identical to string and list operations. The only differences worth noting are that the +, *, and slicing operations return new tuples when applied to tuples, and that tuples don’t provide the same methods you saw for strings, lists, and dictionaries. If you want to sort a tuple, for example, you will generally have to either first convert it to a list to gain access to a sorting method call and make it as a mutable object, or use the newer sorted built-in that accepts any sequence object:
Here, the list and tuple built-in functions are used to convert the object to a list and then back to a tuple; both calls make new objects, but the effect is like a conversion.
List comprehensions can also be used to convert tuples. The following, for example, makes a list from a tuple, adding 20 to each item along the way:
List comprehensions are really sequence operations—they always build new lists, but they may be used to iterate over any sequence objects, including tuples, strings, and other lists. As we’ll see later in the book, they even work on some things that are not physically stored sequences—any iterable objects will do, including files, which are automatically read line by line.
Although tuples don’t have the same methods as lists and strings, they do have two of their own as of Python 2.6 and 3.0—index and count works as they do for lists, but they are defined for tuple objects:
Prior to 2.6 and 3.0, tuples have no methods at all—this was an old Python convention for immutable types, which was violated years ago on grounds of practicality with strings, and more recently with both numbers and tuples.
Also, note that the rule that tuple immutability applies only to the top level of the tuple itself, not to their contents. A list inside a tuple, for instance, can be changed as usual:
For most programs, this one-level-deep immutability is enough for common tuple roles. Which, coincidentally, brings us to the next section.


0 Comments