New course! Every coder should learn Generative AI!
Try a free lesson0
Why is the result 8?
list = [1, 1, 2, 3, 5, 8, 13] print(list[list[4]])
13 Answers
+ 3
list[4]=5 #note that indexing starts from 0.
Now list[5]=8
+ 3
When we print(list [4]) we get 5 as output
. It's in the forth postion . I think u know that it's postion starts from zero. So in thies it will first evaluate s the list [4] and the result of that is 5 next it will evaluate list [5] so the output is 8 .
In the above code u can understand easily.
Print(list[list [4]])
# list[ 4 ] becomes 5
Print (list [5])
👉8
+ 1
list[4]=5
list[5]=8
print(list[list[4]])
print(list[5])
output
8
Firat inner list[4] is calculated than outer list[5]
0
still, don't get it
0
list[4] means value at 4th index and the index starts from 0.
So value at 4th index is 5.
Now list[list[4]] = list[5] because we have solved it as shown above. Now value at 5th index is 8.
Hope it's clear now.
0
list[list[4]] = means list[5] ?
so
list[list[1]] = list[2] correct?
0
So why the result is 2 for this?
list = [1, 1, 2, 3, 5, 8, 13]
print(list[list[2]])
0
Dean Bartusek
No,
list[4] give us element of list of index 4.that is 5.
Than in outer list this 5 becomes list[5] now element at index 5 in your list is 8.
If you still couldn't understand than I suggest you to first complete list lesson on sololearn
0
Dean Bartusek you have not understood indexing well. Take a look at this link https://www.hackerearth.com/practice/notes/samarthbhargav/a-quick-intro-to-indexing-in-JUMP_LINK__&&__python__&&__JUMP_LINK/
0
see now, thank you
0
list[4] evaluates as 5
making the expression list[5]
which evaluates to 8
0
Because of indexing on the inner list[4] the position is number 5 so on the outer list its list[5] and the indexing position 5 is number 8
0
The first list[] started counting at 0 then the second list[] started counting at 1 then to 5 because list[list[4]] returns index 5.