Python Module 2 Quiz | SoloLearn: Learn to code for FREE!

+1

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[4]]) 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[4]]]) 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 PM

Mike Harrold

6 Answers

New Answer

+7

it's not like that like 1+ from inner to outer list Basically inner list[4] returns 5,now 5 becomes index for list[5] so this returns 8 now 8 becomes index for the outermost list like list[8] which is 15

+2

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.

+2

Abhay has provided the answer, but I will try to explain in a different way. list[4] will return 5 list[list[4]] = list[5] will return 8 list[list[list[4]]] = list[5] = list[8] return 15

+2

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?

+1

Mike Harrold Yes

+1

Thank you very much every one.