Python Module 2 Quiz
I have a question regarding the nested list question that I am unable to find the answer to. Here is the question: What is the output of this code? list = [1, 1, 2, 3, 5, 8, 13] print(list[list]) Now after some basic guessing I know the answer is 8. But when testing things I came across something weird. If the question were posted as such: What is the output of this code? list = [1, 1, 2, 3, 5, 8, 13, 14, 15, 16] print(list[list[list]]) The answer is now 15. Can any one tell me why in the first one we add one for the nested list but for the next nested list we add 4? Thank you in advance for your patients in my inability to google properly.
5/9/2020 10:38:24 PMMike Harrold
6 AnswersNew Answer
it's not like that like 1+ from inner to outer list Basically inner list returns 5,now 5 becomes index for list so this returns 8 now 8 becomes index for the outermost list like list which is 15
Thats what I am missing. What is adding three to make the new index location 8. Why is it not 6? Sorry I am really new at this.
Abhay has provided the answer, but I will try to explain in a different way. list will return 5 list[list] = list will return 8 list[list[list]] = list = list return 15
So are are you saying that the number in the index position is being supplanted by the number that it is pointing to and then referencing that new position?
Mike Harrold Yes
Thank you very much every one.