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
# 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))
OutputTuple = (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
# 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])
OutputTuple = (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
# 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])
OutputTuple 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
# 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)
.
Python3
# 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))
Similar Reads
Append Tuple To A Set With Tuples Tuples and sets in Python have some key differences. Tuples are ordered collections of elements that can't be changed once created, denoted by parentheses, and they allow duplicates. You can access specific elements using indexing and slicing. On the other hand, sets are unordered collections of uni
4 min read
Flatten tuple of List to tuple - Python The task of flattening a tuple of lists to a tuple in Python involves extracting and combining elements from multiple lists within a tuple into a single flattened tuple. For example, given tup = ([5, 6], [6, 7, 8, 9], [3]), the goal is to flatten it into (5, 6, 6, 7, 8, 9, 3). Using itertools.chain(
3 min read
Python - Convert Tuple to Tuple Pair Sometimes, while working with Python Tuple records, we can have a problem in which we need to convert Single tuple with 3 elements to pair of dual tuple. This is quite a peculiar problem but can have problems in day-day programming and competitive programming. Let's discuss certain ways in which thi
10 min read
Python | Replace duplicates in tuple Sometimes, while working with Python tuples, we can have a problem in which we need to remove tuples elements that occur more than one times and replace duplicas with some custom value. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() + list comprehension The c
5 min read
Python - Union of Tuples Sometimes, while working with tuples, we can have a problem in which we need union of two records. This type of application can come in Data Science domain. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using set() + "+" operator This task can be performed using union f
2 min read
Python Update Tuples Tuples in Python are often introduced as immutable objects, meaning once a tuple is created, its contents cannot be modified or updated directly. However, there are techniques that allow us to "update" a tuple if we need to change its contents. This article will explore these methods and show you ho
3 min read
Python - Clearing a tuple Sometimes, while working with Records data, we can have a problem in which we may require to perform clearing of data records. Tuples, being immutable cannot be modified and hence makes this job tough. Let's discuss certain ways in which this task can be performed. Method #1 : Using list() + clear()
4 min read
Unpacking Nested Tuples-Python The task of unpacking nested tuples in Python involves iterating through a list of tuples, extracting values from both the outer and inner tuples and restructuring them into a flattened format. For example, a = [(4, (5, 'Gfg')), (7, (8, 6))] becomes [(4, 5, 'Gfg'), (7, 8, 6)].Using list comprehensio
3 min read
Python | Chunk Tuples to N Sometimes, while working with data, we can have a problem in which we may need to perform chunking of tuples each of size N. This is popular in applications in which we need to supply data in chunks. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension
4 min read
Python | Add dictionary to tuple Sometimes, while working with data, we can have a problem in which we need to append to a tuple a new record which is of form of Python dictionary. This kind of application can come in web development domain in case of composite attributes. Let's discuss certain ways in which this task can be perfor
4 min read