Python Tuple
Python’s tuples are similar to lists, except that the elements of a tuple cannot be modified.
Tuples use parentheses, lists use square brackets.
Creating a tuple is simple: just add elements inside parentheses, separated by commas.
As in the following example:
Examples (Python 2.0+)
Create an empty tuple
tup1 = ()
When a tuple contains only one element, you need to add a comma after the element.
tup1 = (50,)
Tuples are similar to strings: indexing starts from 0, and they can be sliced, concatenated, etc.
Access Tuples
Tuples can use index subscripts to access values in the tuple, as in the following example:
Examples (Python 2.0+)
The output of the above example is:
tup1[0]: physics tup2[1:5]: (2, 3, 4, 5)
Modify Tuples
The element values in a tuple are not allowed to be modified, but we can concatenate and combine tuples, as in the following example:
Examples (Python 2.0+)
The output of the above example is:
(12, 34.56, 'abc', 'xyz')
Delete Tuples
The element values in a tuple are not allowed to be deleted, but we can use the del statement to delete the entire tuple, as in the following example:
Examples (Python 2.0+)
After the tuple in the above example is deleted, outputting the variable will produce an exception message, as shown below:
('physics', 'chemistry', 1997, 2000)
After deleting tup :
Traceback (most recent call last):
File "test.py", line 9, in <module>
print tup
NameError: name 'tup' is not defined
Tuple Operators
Like strings, tuples can be operated on with the + and * signs. This means they can be combined and copied, and the operation produces a new tuple.
| Python Expression | Result | Description |
|---|---|---|
| len((1, 2, 3)) | 3 | Count the number of elements |
| (1, 2, 3) + (4, 5, 6) | (1, 2, 3, 4, 5, 6) | Concatenation |
| ('Hi!',) * 4 | ('Hi!', 'Hi!', 'Hi!', 'Hi!') | Replication |
| 3 in (1, 2, 3) | True | Whether an element exists |
| for x in (1, 2, 3): print x, | 1 2 3 | Iteration |
Tuple Indexing and Slicing
Because a tuple is also a sequence, we can access elements at specific positions in the tuple, and we can also slice a segment of elements in the index, as shown below:
Tuple:
L = ('spam', 'Spam', 'SPAM!')
| Python Expression | Result | Description |
|---|---|---|
| L[2] | 'SPAM!' | Read the third element |
| L[-2] | 'Spam' | Read in reverse, read the second-to-last element |
| L[1:] | ('Spam', 'SPAM!') | Slice elements |
No Closing Delimiter
Any objects without enclosing delimiters, separated by commas, default to a tuple, as in the following example:
Examples (Python 2.0+)
The result of running the above example is:
abc -4.24e+93 (18+6.6j) xyz Value of x , y : 1 2
Tuple Built-in Functions
Python tuples contain the following built-in functions:
| No. | Method and Description |
|---|---|
| 1 | cmp(tuple1, tuple2) Compares two tuple elements. |
| 2 | len(tuple) Counts the number of elements in a tuple. |
| 3 | max(tuple) Returns the maximum value of elements in a tuple. |
| 4 | min(tuple) Returns the minimum value of elements in a tuple. |
| 5 | tuple(seq) Converts a list to a tuple. |