Search⌘ K
AI Features

Tuple Varieties

Explore different types of Python tuples such as tuple of tuples, embedded tuples, and lists of tuples. Learn how to unpack tuples and sort complex tuple structures using the key parameter and itemgetter function. This lesson helps you handle tuple data effectively for practical programming challenges.

A tuple of tuples

It is possible to create a tuple of tuples, as shown below:

Python 3.8
a = (1, 3, 5, 7, 9)
b = (2, 4, 6, 8, 10)
c = (a, b)
print(c[0][0], c[1][2]) # 0th element of 0th tuple, 2nd ele of 1st tuple
records = (
('Priyanka', 24, 3455.50), ('Shailesh', 25, 4555.50),
('Subhash', 25, 4505.50), ('Sugandh', 27, 4455.55) )
print(records[0][0], records[0][1], records[0][2])
print(records[1][0], records[1][1], records[1][2])
for n, a, s in records :
print(n, a, s)

Embedded tuple

A tuple can be embedded in another tuple, as shown below:

Python 3.8
x = (1, 2, 3, 4)
y = (10, 20, x, 30)
print(y) # outputs (10, 20, (1, 2, 3, 4), 30)

Unpacking a tuple

It is possible to unpack a tuple within a tuple using the * operator, as shown ...