How we can use Python Tuple within a Tuple?

ProgrammingPythonServer Side Programming

A Tuple can be used within a Tuple easily. Any item of a Tuple can be a Tuple. Tuples are sequences i.e. a collection of objects which ordered and immutable. To access the Tuple within a Tuple, use the square brackets and the index number of that specific inner Tuple.

A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The main difference between the tuples and the lists is that the tuples cannot be changed unlike lists. Tuples use parentheses, whereas lists use square brackets.

Create a basic Tuple

Let us first create a basic Tuple with integer elements and then move towards Tuples within a Tuple

Example

# Creating a Tuple mytuple = (20, 40, 60, 80, 100) # Displaying the Tuple print("Tuple = ",mytuple) # Length of the Tuple print("Tuple Length= ",len(mytuple))

Output

Tuple = (20, 40, 60, 80, 100)
Tuple Length= 5

Creating Tuples within a Tuple

In this example, we will create a Tuple with integer elements. Within that, we will add an inner tuple (18, 19, 20) −

Example

# Creating a Tuple mytuple = (20, 40, (18, 19, 20), 60, 80, 100) # Displaying the Tuple print("Tuple = ",mytuple) # Length of the Tuple print("Tuple Length= ",len(mytuple))

Output

Tuple = (20, 40, (18, 19, 20), 60, 80, 100)
Tuple Length= 6

Above, we have create a Tuple with total 6 elements. One of these elements are actually a Tuple i.e (18, 19, 20), but counted by the len() method as one tuple.

Accessing Tuples within a Tuple

In this example, we will create a Tuple with integer elements. Within that, we will add an inner tuple and access it using the square brackets and the specific index number −

Example

# Creating a Tuple mytuple = (20, 40, (18, 19, 20), 60, 80, 100) # Displaying the Tuple print("Tuple = ",mytuple) # Length of the Tuple print("Tuple Length= ",len(mytuple)) # Display the Tuple within a Tuple print("Tuple within a Tuple = ",mytuple[2])

Output

Tuple = (20, 40, (18, 19, 20), 60, 80, 100)
Tuple Length= 6
Tuple within a Tuple = (18, 19, 20)

Access specific elements of Inner Tuples

In this example, we will create a Tuple with string elements. Within that, we will add an inner tuple. The elements are accessed using the square brackets and the specific index number. But, if you want to access the elements of the Inner Tuples, use their internal index −

Example

# Creating a List mytuple = ("Rassie", "Aiden", ("Dwaine", "Beuran", "Allan"), "Peter") # Displaying the Tuple print("Tuple = ",mytuple) # List length print("Tuple = ",len(mytuple)) # Display the Tuple within a Tuple print("Tuple within a Tuple = ",mytuple[2]) # Display the inner tuple elements one-by-one Tuple within a Tuple print("Inner Tuple 1st element = ",mytuple[2][0]) print("Inner Tuple 2nd element = ",mytuple[2][1]) print("Inner Tuple 3rd element = ",mytuple[2][2])

Output

Tuple = ('Rassie', 'Aiden', ('Dwaine', 'Beuran', 'Allan'), 'Peter')
Tuple = 4
Tuple within a Tuple = ('Dwaine', 'Beuran', 'Allan')
Inner Tuple 1st element = Dwaine
Inner Tuple 2nd element = Beuran
Inner Tuple 3rd element = Allan
raja
Updated on 11-Aug-2022 08:23:03

Advertisements