+ 1
What's the usage of print(list[list[int]])
While going through the question for Python, I have seen a question with the following code: CODE: list = [1, 2, 3, 4, 5, 6] print(list[list[1]]) OUTPUT : 3 I have tried it, and tried to understand it but I don't get it. Can someone explain please ?
2 Answers
+ 2
You have start with the inner most [ ] :
list[1] - > the item on index 1 is the value 2
So list[1] = 2
The next one is then
list[2] - > gives the value on index 2, which is the value 3
That's all.
+ 2
Check the list from inside first, list[1].
list[1] is 2. So list[list[1]] is list[2].
list[2] is 3.
list[list[1]] == list[2] == 3