Open In App

Tuple within a Tuple in Python

In the realm of Python programming, tuples offer a flexible solution for organizing data effectively. By allowing the inclusion of other tuples within them, tuples further enhance their capabilities. In this article, we will see how we can use tuple within a tuple in Python.

Using Tuple within a Tuple in Python

Below are some of the ways by which we can understand about tuple within a tuple in Python:



Creating Tuples within a Tuple

In this example, a tuple named mytuple is created with elements (2, 4, (8, 7, 30), 6, 220, 10, 77, 54). The program then prints the tuple and its length using len(mytuple).




# Creating a Tuple
mytuple = (2, 4, (8, 7, 30), 6, 220, 10, 77, 54)
 
# Displaying the Tuple
print("Tuple =", mytuple)
 
# Tuple Length
print("Tuple Length =", len(mytuple))

Output

Tuple = (2, 4, (8, 7, 30), 6, 220, 10, 77, 54)
Tuple Length = 8

Accessing Tuples within a Tuple

In this example, a tuple named mytuple is created with elements (50, 123, 124, (195, 230, 640), 850, 1000). The program prints the tuple and its length using len(mytuple). Additionally, it displays the tuple within a tuple by accessing the sixth element using indexing (mytuple[5]).




# Creating a Tuple
mytuple = (50, 123, 124, (195, 230, 640), 850, 1000)
 
# Displaying the Tuple
print("Tuple =", mytuple)
 
# Tuple Length
print("Tuple Length =", len(mytuple))
 
# Displaying the Tuple within a Tuple
print("Tuple within a Tuple =", mytuple[5])

Output
Tuple = (50, 123, 124, (195, 230, 640), 850, 1000)
Tuple Length = 6
Tuple within a Tuple = 1000

Accessing Specific Elements of Inner Tuples

In this example, a tuple named mytuple is created with elements (“geeks”, “for”, (“geeks”, “python”, “java”), “90days of completion”, “gfgchallenge”). The program displays the tuple within a tuple using indexing (mytuple[2]). Furthermore, it extracts and prints the inner tuple elements individually: 1st element (“geeks”), 2nd element (“python”), and 3rd element (“java”) using nested indexing (mytuple[2][0], mytuple[2][1], mytuple[2][2]).




# Creating a Tuple
mytuple = ("geeks", "for", ("geeks", "python", "java"),
           "90days of completion", "gfgchallenge")
 
# Displaying the Tuple within a Tuple
print("Tuple within a Tuple =", mytuple[2])
 
# Displaying the inner tuple elements one-by-one
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 within a Tuple = ('geeks', 'python', 'java')
Inner Tuple 1st element = geeks
Inner Tuple 2nd element = python
Inner Tuple 3rd element = java

Deletion Operation in Python Tuple Within A Tuple

In this example, the original tuple of tuples named data is defined with elements (‘dog’, 30), (‘cat’, 20), (‘bird’, 10), and (‘fish’, 25). The program utilizes a list comprehension to delete the tuple containing ‘bird’. The resulting modified tuple is then printed using print(data).




# Original tuple of tuples
data = (
    ('dog', 30),
    ('cat', 20),
    ('bird', 10),
    ('fish', 25)
)
 
# Deletion: Removing the tuple containing 'bird'
data = tuple(item for item in data if item[0] != 'bird')
print(data)

Output
(('dog', 30), ('cat', 20), ('fish', 25))

Sorting in Python Tuple Within A Tuple

In this example, the original tuple of tuples named data is defined with elements (‘dog’, 30), (‘cat’, 20), (‘bird’, 10), and (‘fish’, 25). The program uses the sorted() function to sort the tuples based on their second element (quantity) in ascending order. The resulting modified tuple is then printed using print(data).




# Original tuple of tuples
data = (
    ('dog', 30),
    ('cat', 20),
    ('bird', 10),
    ('fish', 25)
)
 
# Sorting: Sorting the tuples based on the second element (quantity)
data = tuple(sorted(data, key=lambda x: x[1]))
 
print(data)

Output
(('bird', 10), ('cat', 20), ('fish', 25), ('dog', 30))

Article Tags :