Section 6.16. Tuples


6.16. Tuples

Tuples are another container type extremely similar in nature to lists. The only visible difference between tuples and lists is that tuples use parentheses and lists use square brackets. Functionally, there is a more significant difference, and that is the fact that tuples are immutable. Because of this, tuples can do something that lists cannot do . . . be a dictionary key. Tuples are also the default when dealing with a group of objects.

Our usual modus operandi is to present the operators and built-in functions for the more general objects, followed by those for sequences and conclude with those applicable only for tuples, but because tuples share so many characteristics with lists, we would be duplicating much of our description from the previous section. Rather than providing much repeated information, we will differentiate tuples from lists as they apply to each set of operators and functionality, then discuss immutability and other features unique to tuples.

How to Create and Assign Tuples

Creating and assigning tuples are practically identical to creating and assigning lists, with the exception of empty tuplesthese require a trailing comma ( , ) enclosed in the tuple delimiting parentheses ( ( ) ) to prevent them from being confused with the natural grouping operation of parentheses. Do not forget the factory function!

>>> aTuple = (123, 'abc', 4.56, ['inner', 'tuple'], 7-9j) >>> anotherTuple = (None, 'something to see here') >>> print aTuple (123, 'abc', 4.56, ['inner', 'tuple'], (7-9j)) >>> print anotherTuple (None, 'something to see here') >>> emptiestPossibleTuple = (None,) >>> print emptiestPossibleTuple (None,) >>> tuple('bar') ('b', 'a', 'r')


How to Access Values in Tuples

Slicing works similarly to lists. Use the square bracket slice operator ( [ ] ) along with the index or indices.

>>> aTuple[1:4] ('abc', 4.56, ['inner', 'tuple']) >>> aTuple[:3] (123, 'abc', 4.56) >>> aTuple[3][1] 'tuple'


How to Update Tuples

Like numbers and strings, tuples are immutable, which means you cannot update them or change values of tuple elements. In Sections 6.2 and 6.3.2, we were able to take portions of an existing string to create a new string. The same applies for tuples.

>>> aTuple = aTuple[0], aTuple[1], aTuple[-1] >>> aTuple (123, 'abc', (7-9j)) >>> tup1 = (12, 34.56) >>> tup2 = ('abc', 'xyz') >>> tup3 = tup1 + tup2 >>> tup3 (12, 34.56, 'abc', 'xyz')


How to Remove Tuple Elements and Tuples

Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded.

To explicitly remove an entire tuple, just use the del statement to reduce an object's reference count. It will be deallocated when that count is zero. Keep in mind that most of the time one will just let an object go out of scope rather than using del, a rare occurrence in everyday Python programming.

del aTuple




Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net