A quiz related question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

A quiz related question

list=[1,1,2,3,5,8,13] print(list[list[4]]) Output:- 8 How it's output is 8?, if i replace [4] with any other number it shows error...

15th Mar 2017, 6:17 AM
Smile garg
Smile garg - avatar
6 Answers
+ 21
it's simple: list [list [4]] if you look at the list, you can see that the 4 element in the list is the 5 (index starts at 0!!!) now you have list [5] now the 5 element of the list is the 8 (count from 0 again) 8 is the output
15th Mar 2017, 6:29 AM
Kamil
Kamil - avatar
+ 4
list[4] is 5 so list[list[4]] is list[5] which is 8 If you put 5 or 6 like that list[list[5]] there will be an out of bound exception as list has 7 values so no value at the index 8 or 13
15th Mar 2017, 6:26 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
Got It! Thanks !
15th Mar 2017, 9:22 AM
Smile garg
Smile garg - avatar
+ 2
No, it was not said « add » It is access, so list[4] will get the 5th value of list, here 5 So list[list[4]] also means list[5] which is the 6th element of list, 8
16th Oct 2022, 9:40 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
0
Ah okay, so list[list[...]] is equal to "add [...] to list position" ? Wtf, why is that so? Sololearn didn't teach it to us, but asks for it in the last chapter of the "Lists quiz"...
16th Oct 2022, 9:27 AM
Ghggh Jfdhhfg
Ghggh Jfdhhfg - avatar
0
Ahhh, I was so dumb! List [***list[4]***] is putting the 4th place of the list in ***list [4]*** So in a List = [78, 45, 99, 2] List [4] is "2". so list [list[4]] is list [2]
16th Oct 2022, 9:31 AM
Ghggh Jfdhhfg
Ghggh Jfdhhfg - avatar