Why is the result 8? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is the result 8?

list = [1, 1, 2, 3, 5, 8, 13] print(list[list[4]])

8th Apr 2020, 8:20 AM
Dean Bartusek
Dean Bartusek - avatar
13 Answers
+ 3
list[4]=5 #note that indexing starts from 0. Now list[5]=8
9th Apr 2020, 11:20 AM
Md. Sazzad Hossain
Md. Sazzad Hossain - avatar
+ 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
9th Apr 2020, 5:42 PM
Vijay(v-star🌟)
Vijay(v-star🌟) - avatar
+ 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]
8th Apr 2020, 8:29 AM
Muhammad Bilal
Muhammad Bilal - avatar
0
still, don't get it
8th Apr 2020, 8:41 AM
Dean Bartusek
Dean Bartusek - avatar
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.
8th Apr 2020, 8:46 AM
Avinesh
Avinesh - avatar
0
list[list[4]] = means list[5] ? so list[list[1]] = list[2] correct?
8th Apr 2020, 8:51 AM
Dean Bartusek
Dean Bartusek - avatar
0
So why the result is 2 for this? list = [1, 1, 2, 3, 5, 8, 13] print(list[list[2]])
8th Apr 2020, 9:01 AM
Dean Bartusek
Dean Bartusek - avatar
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
8th Apr 2020, 9:03 AM
Muhammad Bilal
Muhammad Bilal - avatar
8th Apr 2020, 9:04 AM
Avinesh
Avinesh - avatar
0
see now, thank you
8th Apr 2020, 9:17 AM
Dean Bartusek
Dean Bartusek - avatar
0
list[4] evaluates as 5 making the expression list[5] which evaluates to 8
8th Apr 2020, 10:39 AM
Iyad Ahmed
Iyad Ahmed - avatar
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
9th Apr 2020, 10:17 AM
rachel oyugi
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.
10th Apr 2020, 7:28 AM
Heather
Heather - avatar