- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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?
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
- Related Articles
- How we can update a Python tuple element value?
- How can I use Multiple-tuple in Python?
- How can I append a tuple into another tuple in Python?
- How we can create a dictionary from a given tuple in Python?
- How can I subtract tuple of tuples from a tuple in Python?
- How do we define tuple in Python?
- How can I do Python Tuple Slicing?
- How can I create a non-literal python tuple?
- How I can convert a Python Tuple into Dictionary?
- How can I convert a Python tuple to string?
- How do we grep a particular keyword from Python tuple?
- How can I convert Python strings into tuple?
- How can I create a Python tuple of Unicode strings?
- How can I remove items out of a Python tuple?
- How can I define duplicate items in a Python tuple?
