Skip to content

Python3 Tuples

Python tuples are similar to lists, but the difference is that tuple elements 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.

>>> tup1 = ('Google', 'Runoob', 1997, 2000)
>>> tup2 = (1, 2, 3, 4, 5 )
>>> tup3 = "a", "b", "c", "d"   #  Parentheses are optional
>>> type(tup3)
<class 'tuple'>

Creating an empty tuple:

tup1 = ()

When a tuple contains only one element, you need to add a comma after the element, otherwise the parentheses will be treated as an operator:

>>> tup1 = (50)
>>> type(tup1)     # Without comma, the type is int
<class 'int'>

>>> tup1 = (50,)
>>> type(tup1)     # With comma, the type is tuple
<class 'tuple'>

Tuples are similar to strings: subscript indices start at 0, and they can be sliced and combined.


Tuples can be accessed using subscript indices, as shown in the example below:

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

Output of the above example:

tup1[0]:  Google
tup2[1:5]:  (2, 3, 4, 5)

Tuple element values cannot be modified, but we can concatenate and combine tuples, as shown in the example below:

#!/usr/bin/python3
 
tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
 
# The following modification of tuple elements is illegal.
# tup1[0] = 100
 
# Create a new tuple
tup3 = tup1 + tup2
print (tup3)

Output of the above example:

(12, 34.56, 'abc', 'xyz')

Tuple element values cannot be deleted, but we can use the del statement to delete the entire tuple, as shown in the example below:

#!/usr/bin/python3
 
tup = ('Google', 'Runoob', 1997, 2000)
 
print (tup)
del tup
print ("Deleted tuple tup: ")
print (tup)

After the tuple is deleted, printing the variable will raise an exception, with output as shown below:

Deleted tuple tup: 
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    print (tup)
NameError: name 'tup' is not defined

Like strings, tuples can be operated on using +, += and * signs. This means they can be combined and copied, and the operation will produce a new tuple.

Python Expression Result Description
len((1, 2, 3))
3 Count elements
>>> a = (1, 2, 3)
>>> b = (4, 5, 6)
>>> c = a+b
>>> c
(1, 2, 3, 4, 5, 6)
(1, 2, 3, 4, 5, 6) Concatenation. c is a new tuple containing all elements from a and b.
>>> a = (1, 2, 3)
>>> b = (4, 5, 6)
>>> a += b
>>> a
(1, 2, 3, 4, 5, 6)
(1, 2, 3, 4, 5, 6) Concatenation. a becomes a new tuple containing all elements from a and b.
('Hi!',) * 4
('Hi!', 'Hi!', 'Hi!', 'Hi!') Repetition
3 in (1, 2, 3)
True Element exists
for x in (1, 2, 3): 
    print (x, end=" ")
1 2 3 Iteration

Since a tuple is also a sequence, we can access elements at specific positions in a tuple and slice a range of elements, as shown below:

Tuple:

tup = ('Google', 'Runoob', 'Taobao', 'Wiki', 'Weibo','Weixin')

Python Expression Result Description
tup[1] ‘Runoob’ Read the second element
tup[-2] ‘Weibo’ Reverse read, read the second-to-last element
tup[1:] (‘Runoob’, ‘Taobao’, ‘Wiki’, ‘Weibo’, ‘Weixin’) Slice elements, all elements starting from the second.
tup[1:4] (‘Runoob’, ‘Taobao’, ‘Wiki’) Slice elements, from the second to the fourth element (index 3).

Running example:

>>> tup = ('Google', 'Runoob', 'Taobao', 'Wiki', 'Weibo','Weixin')
>>> tup[1]
'Runoob'
>>> tup[-2]
'Weibo'
>>> tup[1:]
('Runoob', 'Taobao', 'Wiki', 'Weibo', 'Weixin')
>>> tup[1:4]
('Runoob', 'Taobao', 'Wiki')
>>> 

Python tuples include the following built-in functions:

No. Method & Description Example
1 len(tuple)
Counts the number of tuple elements.
>>> tuple1 = ('Google', 'Runoob', 'Taobao')
>>> len(tuple1)
3
>>> 
2 max(tuple)
Returns the maximum value of tuple elements.
>>> tuple2 = ('5', '4', '8')
>>> max(tuple2)
'8'
>>> 
3 min(tuple)
Returns the minimum value of tuple elements.
>>> tuple2 = ('5', '4', '8')
>>> min(tuple2)
'4'
>>> 
4 tuple(iterable)
Converts an iterable series to a tuple.
>>> list1= ['Google', 'Taobao', 'Runoob', 'Baidu']
>>> tuple1=tuple(list1)
>>> tuple1
('Google', 'Taobao', 'Runoob', 'Baidu')

The immutability of tuples means that the content of the memory the tuple points to cannot be changed.

>>> tup = ('r', 'u', 'n', 'o', 'o', 'b')
>>> tup[0] = 'g'     # Modifying elements is not supported
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> id(tup)     # Check memory address
4440687904
>>> tup = (1,2,3)
>>> id(tup)
4441088800    # Memory address is different

From the above example, we can see that the reassigned tuple tup is bound to a new object, not modifying the original object.