What do the extra brackets do? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

What do the extra brackets do?

What is the output of this code? list = [1, 1, 2, 3, 5, 8, 13] print(list[list[4]]) Why does it print 8?

22nd Feb 2020, 12:52 AM
Oksana
9 Answers
+ 10
list[4] = 5 list[list[4]] = list[5] = 8
22nd Feb 2020, 1:20 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 6
Solve it from the inside out. list[4] is the element at index position 4, which is 5. So list[list[4]] is the same as list[5] list[5] is the element at index position 5, which is 8. 😊
22nd Feb 2020, 1:24 AM
David Ashton
David Ashton - avatar
+ 5
list[4] accesses the fifth element of list, which is 5. list[list[4]] hence accesses list[5], which is the sixth element of list, 8.
22nd Feb 2020, 1:20 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
Three answers for the price of one 😄
22nd Feb 2020, 1:26 AM
David Ashton
David Ashton - avatar
+ 4
list = [1, 1, 2, 3, 5, 8, 13] print(list[list[4]]) 1. list's index start from 0 then 1 then 2... 2. ' list[4] ' will calculated first list[4] = 5 3. then list[ list[4] ] = list[5] and, list[5] = 8 thats why it prints 8
23rd Feb 2020, 7:10 AM
BlackShadow334
BlackShadow334 - avatar
+ 2
Good question,,,, list[0] means the first value of the array. So, list[4] is the 5th value of the array means 5; So, list[list[4]]->list[5] means the 6th value of the array means 8; So, the answer is 8;
23rd Feb 2020, 11:42 AM
ISTIAQUE ZAMAN
ISTIAQUE ZAMAN - avatar
+ 1
list[3] is the element at index position 3, the quarter position, because in programming you start with the 0. The first element would be list[0] and list[1] would be the second element. exmple: list = [h, e, l, l, o] print(list[list[4]]) output: the O of the list
24th Feb 2020, 2:55 PM
AlexRos
AlexRos - avatar
0
Thank you everyone!!! I get it now!
24th Feb 2020, 5:37 PM
Oksana
- 1
you know the answer dude...
23rd Feb 2020, 5:01 PM
Suraj Kumar
Suraj Kumar - avatar