Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - NESTED TUPLES


'Nested Tuples' are the one in which, a block of Tuple can contain another Tuple.


Say for example, let us take the below Tuple.


java_Collections

So, if you see the second location or index/position '1' of the above Tuple. There is another Tuple of two elements containing the names 'Satyam' and 'Rishab' stored in it.


This Tuple is an example of 'Nested Tuple'.


How to create and access the elements of Nested Tuples?


Let us create the above 'Nested Tuple'.


Example :


x = ("Mohan", ("Satyam", "Rishab") , "Paul", "Kriti", "Salim")
print("The first element of the nested Tuple is ", x[1][0])
print("The second element of the nested Tuple is ", x[1][1])


Output :



  The first element of the nested Tuple is Satyam
  The second element of the nested Tuple is Rishab

So, in the above example, we have declared a Nested Tuple and initialised it to 'x'.


x = ("Mohan", ("Satyam", "Rishab") , "Paul", "Kriti", "Salim")

Now, let us see the diagram,


java_Collections

And as we can see, the names 'Satyam' and 'Rishab' is located at the second location of 'x'.


So, to access 'Satyam', we need to goto index '1' of the Tuple and then goto the index of 'Satyam' i.e. Index '0'.


i.e. x[1][0]


And thus the print statement prints 'Satyam'.


print("The first element of the nested Tuple is ", x[1][0])

Similarly, to access 'Rishab', we need to goto index '1' of the Tuple and then goto the index of 'Rishab' i.e. Index '1'.


i.e. x[1][1]


And thus the print statement prints 'Satyam'.


print("The second element of the nested Tuple is ", x[1][1])

A deeper Nested Tuple


Now, what if we want to find out the first letter of 'Rishab' or the second letter of Paul.


Example :


x = ("Mohan", ("Satyam", "Rishab") , "Paul", "Kriti", "Salim")
print("The first letter of Rishab is ", x[1][1][0])
print("The second letter of Paul is ", x[2][1])     


Output :



  The first letter of Rishab is R
  The second letter of Paul is a

So, in the above example, we have declared a Nested Tuple and initialised it to 'x'.


x = ("Mohan", ("Satyam", "Rishab") , "Paul", "Kriti", "Salim")

So, we have the same diagram,


java_Collections

Similarly, to access the first letter of i.e. 'R' of 'Rishab',


  1. We need to goto 'Rishab'

  2. And as we have seen, to access 'Rishab', we need to goto index '1' of the Tuple and then goto the index of 'Rishab' i.e. Index '1'.

  3. Which would be 'x[1][1]'. And 'x[1][1]' is the location where 'Rishab' lies.

  4. Now, if we further subdivide 'Rishav'. It would be,

    java_Collections

  5. And the 'R' of 'Rishav' lies in the '0'th location.

  6. So, the finally to reach 'R' of 'Rishav', the location is 'x[1][1][0]'.

  7. Where 'x[1][1]' is the location of 'Rishav' and '0' is the location of 'R'. Which is 'x[1][1][0]'.

And thus the print statement, prints the first letter of 'Rishav'.


print("The first letter of Rishab is ", x[1][1][0])

And the same way, the print statement prints the second letter of 'Paul', i.e. 'a'.


print("The second letter of Paul is ", x[2][1])

Note : Since, a Tuple is Immutable. We will not be able to make any changes to the Tuple.i.e. We will not be able to insert or update or remove or even sort any elements in the 'Tuple'.