Nested Tuple in Python
If you are a Python learner, this page contains all the information that helps you know about nested tuple and how to access it in python.
What is a Tuple?
Multiple items can be stored in a single variable by using tuples. One of the four built-in data types in Python for storing data collections is the tuple; the other three are list, set, and dictionary, each with a unique set of features and applications. A tuple is an unchanging, ordered collection.
A tuple is similar to a list, which helps in storing data in python. The major difference between a tuple and a list is that the list is mutable, but the tuple is immutable. In a simple way, we can say, even after declaring a list, we can change the items of the list, but we cannot change the items in a tuple once it is declared. Hence, tuples are immutable, as said above. To declare a list, we make use of square brackets. Similarly, we make use of the parentheses, aka round brackets, in the declaration of a tuple.
Syntax of declaring a Tuple
tuplename = (var_1, var_2, var_3, var_4, ........, var_n)
Example of declaring a tuple
tup = (29, 18, 2, 30)
Program to print a tuple element
tup = (29, 18, 2, 30)
print(tup[1])
Output
Program to explain that the declared tuple cannot be changed
tup = (29, 18, 2, 30)
tup[2] = 19
Output
This is the syntax of a tuple containing n items in it. The commas separate the variables from one another, and the parentheses are used to declare the tuple as mentioned above.
Methods like index and count can be used in combination with tuples as we use them in the case of lists, but there is no method to help replace the items of a tuple once it is declared. So, a tuple is used in situations where the user knows that he doesn't need to change the items of the tuple. Since the items in a tuple cannot be changed, this makes the iterations faster in a tuple when compared to lists. So, it means that the tuples are executed faster than lists. The tuples are also indexed similarly to the lists.
Tuples also follow zero-based indexing, starting from its first element having assigned with the index [0]. When we print a tuple, its items are printed in user-defined order, unlike the sets whose elements are printed in random order. Also, duplicate elements are allowed to be printed in a tuple, while sets don’t allow duplicate elements in their outputs in python language.
Program to print a tuple
tuple = (' A ', ' B ', ' C ', ' D ')
print(tuple)
Output
Program to print a set
s = {12, 16, 20, 15, 10, 15}
print(s)
Output
A tuple's type is defined as the tuple itself while it can store items of different data types such as Boolean, int, and a string. There is a quality in tuples that makes them unique, i.e., a tuple can store items of different data types in it at the same time.
Program of a single tuple declaring both strings and integers
tuple = (" Hello ", 20, " World ", 19)
print(tuple)
Output
What are Nested Tuples?
A tuple having one or more tuples nesting inside it is called a nested tuple. In other words, if a tuple itself contains items that are also tuples, it is known as a nested tuple. To consider an example, let “a = (10, 20, 30, (50, 60))” is a tuple that is declared in python. Here, “a” is called a nested tuple as it contains another tuple “(50, 60)” as one of its items.
The above-given example can be written in another way as, let "b = (50, 60)" is a tuple and "a = (10, 20, 30, b)" is another tuple. Here, we can say that "a" is a nested tuple as it contains tuple “b” nesting inside it. Now, let us see how a tuple looks with two more tuples inside it. "a = ((10, 20, 30), (40, 50, 60))" is a nested tuple with indexing [0] and [1].
Here, we can see the indexing given to both the tuples as items of the main tuple. The items of the sub-tuples are also given indexing starting from the index [0]. This is what a nested tuple looks like in the python language.
Accessing a Nested Tuple
A nested tuple can be accessed similarly to how we access a general tuple stored with its elements. An extra pair of square brackets are used for every increase in dimension of the tuple. Suppose for the tuple “a = (10, 20, 30, (50, 60))”, if we want to access the element “10”, we use the statement “a [0]”. To access the two-dimensional element “50” from the tuple, we use the statement “a [3] [0]”.
Program for accessing a Nested Tuple
Now let us see a program for accessing an element in a nested tuple.
a = (10, 20, 30, (50, 60))
print (a [3] [0])
Output