Tuple within a Tuple in Python
Last Updated :
21 Feb, 2024
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)
.
Python3
mytuple = ( 2 , 4 , ( 8 , 7 , 30 ), 6 , 220 , 10 , 77 , 54 )
print ( "Tuple =" , mytuple)
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]
).
Python3
mytuple = ( 50 , 123 , 124 , ( 195 , 230 , 640 ), 850 , 1000 )
print ( "Tuple =" , mytuple)
print ( "Tuple Length =" , len (mytuple))
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]
).
Python3
mytuple = ( "geeks" , "for" , ( "geeks" , "python" , "java" ),
"90days of completion" , "gfgchallenge" )
print ( "Tuple within a Tuple =" , mytuple[ 2 ])
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)
.
Python3
data = (
( 'dog' , 30 ),
( 'cat' , 20 ),
( 'bird' , 10 ),
( 'fish' , 25 )
)
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)
.
Python3
data = (
( 'dog' , 30 ),
( 'cat' , 20 ),
( 'bird' , 10 ),
( 'fish' , 25 )
)
data = tuple ( sorted (data, key = lambda x: x[ 1 ]))
print (data)
|
Output
(('bird', 10), ('cat', 20), ('fish', 25), ('dog', 30))
Share your thoughts in the comments
Please Login to comment...