'Nested Lists' are the one in which, a block of List can contain another List.
Say for example, let us take the below List.
So, if you see the second location or index/position '1' of the above List. There is another List of two elements containing the names 'Satyam' and 'Rishab' stored in it.
This List is an example of 'Nested List'.
Let us create the above 'Nested List'.
x = ["Mohan", [ "Satyam", "Rishab"] , "Paul", "Kriti", "Salim"] print("The first element of the nested List is ", x[1][0]) print("The second element of the nested List is ", x[1][1])
So, in the above example, we have declared a Nested List and initialised it to 'x'.
Now, let us see the diagram,
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 List and then goto the index of 'Satyam' i.e. Index '0'.
i.e. x[1][0]
And thus the print statement prints 'Satyam'.
Similarly, to access 'Rishab', we need to goto index '1' of the List and then goto the index of 'Rishab' i.e. Index '1'.
i.e. x[1][1]
And thus the print statement prints 'Satyam'.
Now, what if we want to find out the first letter of 'Rishab' or the second letter of Paul.
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])
So, in the above example, we have declared a Nested List and initialised it to 'x'.
So, we have the same diagram,
Similarly, to access the first letter of i.e. 'R' of 'Rishab',
And thus the print statement, prints the first letter of 'Rishav'.
And the same way, the print statement prints the second letter of 'Paul', i.e. 'a'.