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+)

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, 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+)

#!/usr/bin/python tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print "tup1[0]: ", tup1[0] print "tup2[1:5]: ", tup2[1:5]

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+)

#!/usr/bin/python # -*- coding: UTF-8 -*- tup1 = (12, 34.56) tup2 = ('abc', 'xyz') # The following operation of modifying tuple elements is illegal. # tup1[0] = 100 # Create a new tuple tup3 = tup1 + tup2 print tup3

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+)

#!/usr/bin/python tup = ('physics', 'chemistry', 1997, 2000) print tup del tup print "After deleting tup : " print tup

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 ExpressionResultDescription
len((1, 2, 3))3Count 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)TrueWhether an element exists
for x in (1, 2, 3): print x,1 2 3Iteration

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 ExpressionResultDescription
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+)

#!/usr/bin/python print 'abc', -4.24e93, 18+6.6j, 'xyz' x, y = 1, 2 print "Value of x , y : ", x,y

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
1cmp(tuple1, tuple2)
Compares two tuple elements.
2len(tuple)
Counts the number of elements in a tuple.
3max(tuple)
Returns the maximum value of elements in a tuple.
4min(tuple)
Returns the minimum value of elements in a tuple.
5tuple(seq)
Converts a list to a tuple.
Other Extensions