+ 2

Python List Class

What is the output of this code? list = [1, 1, 2, 3, 5, 8, 13] print(list[list[4]]) This makes no sense to me. The answer was 8. Can anyone explain?

23rd Mar 2018, 2:20 AM
Dominic Iannucci
Dominic Iannucci - avatar
4 Answers
+ 5
list[list[4]] list[5] 8 # note that index starts at 0
23rd Mar 2018, 2:27 AM
Pedro Demingos
Pedro Demingos - avatar
+ 2
I did not understand that I needed to compute (list[list[4]]) to (list[5]). That was the missing link. Thank you, Pedro! đŸ‘đŸ»
23rd Mar 2018, 2:31 AM
Dominic Iannucci
Dominic Iannucci - avatar
+ 2
list[4] is fifth element in list (cause indixes start from 0), so [0] is 1 [1] is 1 [2] is 2 [3] is 3 [4] is 5 [5]is 8 [6] is 13. So, list[4] is 5. Now we get print(list[5]) and solve it same way - look for 6th element in list. It is 8. So, output is 8.
23rd Mar 2018, 4:59 AM
Alice Laniakea
Alice Laniakea - avatar
+ 1
Thank you for a more verbose explanation. I think I have it now. You guys (and gals) are amazing! đŸ‘đŸ»đŸ‘đŸ»
23rd Mar 2018, 5:02 AM
Dominic Iannucci
Dominic Iannucci - avatar