Python Tuple
Python tuples are similar to lists, except that the elements of a tuple cannot be modified.
Tuples use parentheses, while lists use square brackets.
Creating a tuple is simple: just add elements inside parentheses and separate them with commas.
The following example:
Example (Python 2.0+)
tup1
=
(
'
physics
'
,
'
chemistry
'
,
1997
,
2000
)
tup2
=
(
1
,
2
,
3
,
4
,
5
)
tup3
=
"
a
"
,
"
b
"
,
"
c
"
,
"
d
"
Create an empty tuple
tup1 = ()
When a tuple contains only one element, a comma must be added after the element
tup1 = (50,)
Tuples are similar to strings; index subscripts start at 0, and they can be sliced, combined, and so on.
Accessing tuples
Tuples can use subscript indices to access their values, as shown below:
Example (Python 2.0+)
#!/usr/bin/python
tup1
=
(
'
physics
'
,
'
chemistry
'
,
1997
,
2000
)
tup2
=
(
1
,
2
,
3
,
4
,
5
,
6
,
7
)
"
tup1[0]:
"
,
tup1
[
0
]
"
tup2[1:5]:
"
,
tup2
[
1
:
5
]
The example above produces the following output:
tup1[0]: physics
tup2[1:5]: (2, 3, 4, 5)
Modifying tuples
The element values in a tuple cannot be modified, but we can concatenate and combine tuples, as shown below:
Example (Python 2.0+)
#!/usr/bin/python
-- coding: UTF-8 --
tup1
=
(
12
,
34.56
)
tup2
=
(
'
abc
'
,
'
xyz
'
)
The following operation that modifies a tuple element is illegal.
tup1[0] = 100
Create a new tuple
tup3
=
tup1
tup2
tup3
The example above produces the following output:
(12, 34.56, 'abc', 'xyz')
Deleting tuples
The element values in a tuple cannot be deleted, but we can use the del statement to delete the entire tuple, as shown below:
Example (Python 2.0+)
#!/usr/bin/python
tup
=
(
'
physics
'
,
'
chemistry
'
,
1997
,
2000
)
tup
del
tup
"
After deleting tup :
"
tup
After the tuple is deleted in the above example, outputting the variable produces an exception message, as shown below:
('physics', 'chemistry', 1997, 2000)
After deleting tup :
Traceback (most recent call last):
File "test.py", line 9, in
print tup
NameError: name 'tup' is not defined
Tuple operators
Like strings, tuples can be operated on with the + and * operators. This means they can be concatenated and repeated, producing a new tuple after the operation.
| 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) | Connection |
| ('Hi!',) * 4 | ('Hi!', 'Hi!', 'Hi!', 'Hi!') | Copy |
| 3 in (1, 2, 3) | True | Whether an element exists |
| for x in (1, 2, 3): print x, | 1 2 3 | Iterate |
Tuple indexing and slicing
Because a tuple is also a sequence, we can access the element at a specified position and slice a range of indices, 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, reading the second-to-last element |
| L[1:] | ('Spam', 'SPAM!') | Slice elements |
No closing delimiter
Any unsigned objects separated by commas default to a tuple, as in the following example:
Example (Python 2.0+)
#!/usr/bin/python
'
abc
'
, -
4.24e93
,
18
6.6j
,
'
xyz
'
x
,
y
=
1
,
2
"
Value of x , y :
"
,
x
,
y
The result of the above example:
abc -4.24e+93 (18+6.6j) xyz
Value of x , y : 1 2
Tuple built-in functions
Python tuples include the following built-in functions
| No. | Method and description |
|---|---|
| 1 | cmp(tuple1, tuple2) |
| Compare two tuple elements. | |
| 2 | len(tuple) |
| Count the number of elements in a tuple. | |
| 3 | max(tuple) |
| Returns the maximum value among the elements of a tuple. | |
| 4 | min(tuple) |
| Returns the minimum value among the elements of a tuple. | |
| 5 | tuple(seq) |
| Convert a list to a tuple. |