How to Create Nested Tuples in Python

In this Python Tuples article we want to learn How to Create Nested Tuples in Python, Tuples are a fundamental data structure in Python, used to store and organize data. tuple is a sequence of values that are immutable and it means that they cannot be changed after the tuple is created. Nested tuples are simply tuples that contain other tuples as elements. in this article we want to talk how to create nested tuples.

 

 

How to Create Nested Tuples in Python

For creating nested tuple you can simply include one or more tuples as elements within a tuple, in the below example we have first created simple tuple t1 containing three integers. after that we have created nested tuple t2 that includes t1 as one of its elements. and lastly we create another nested tuple t3 that includes t2 as one of its elements.

 

 

 

This will be the result

How to Create Nested Tuples in Python
How to Create Nested Tuples in Python

 

 

For accessing the elements of a nested tuple you can use indexing, just as you would with  regular tuple.

 

 

This will be the result

Python Nested Tuples
Python Nested Tuples

 

 

You can also iterate over nested tuple using for loop, just as you would with regular tuple. 

 

 

This will be the result

How to Create Nested Tuples in Python
How to Create Nested Tuples 

 

 

Learn More on Python Tuples

Leave a Comment