Tuple Varieties
Learn about the different varieties of tuples in Python.
We'll cover the following...
A tuple of tuples
It is possible to create a tuple of tuples, as shown below:
Press + to interact
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 tuplerecords = (('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:
Press + to interact
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 ...