Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How we can use Python Tuple within a Tuple?
In Python, a Tuple is an ordered and immutable collection of elements, and tuples are created using parentheses. In this article, we are going to explore all the different ways to use a tuple within a tuple.
Tuple within a Tuple in Python
A tuple can contain elements of any data type, such as another tuple. When one tuple is placed inside another tuple, it is called a nested tuple or a tuple within a tuple. Following is the syntax of creating a Tuple in Python -
outer_tuple = (value1, value2, (nested_value1, nested_value2), value3)
Note: We can access the nested tuple elements using multiple index levels.
Example: Create a Basic Tuple
In this example, we are going to create a basic Tuple with integer elements and then move towards Tuples within a Tuple -
# Creating a Tuple
mytuple = (20, 40, 60, 80, 100)
# Displaying the Tuple
print("Tuple = ", mytuple)
# Length of the Tuple
print("Tuple Length= ",len(mytuple))
Following is the output of the above program -
Tuple = (20, 40, 60, 80, 100) Tuple Length= 5
Creating Tuples within a Tuple
Here in this example, we will create a Tuple with integer elements, and within that tuple, we will add an inner tuple (18, 19, 20) to it -
# 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))
Below is the output of the above program -
Tuple = (20, 40, (18, 19, 20), 60, 80, 100) Tuple Length= 6
Accessing Tuples within a Tuple
In Python, a tuple can contain other tuples as elements. These are called nested tuples. We can access the inner tuple and its elements using indexing.
Each level of nesting requires an additional index. Here is an example of accessing the Tuple within a Tuple -
# 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])
print("Tuple Length= ",len(mytuple))
Following is the output of the above program -
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. If we want to access the elements of the Inner Tuples, then we have to use their internal index -
# 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])
Below is the output of the above program -
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