+ 3

Why does print(list[list[4]]) have a different output to print(list[4])?

Why does print(list[list[4]]) have a different output to print(list[4])? Please help.

15th Mar 2017, 8:10 PM
Jayelar
Jayelar - avatar
7 Respuestas
+ 30
In Python, when you do: my_list[index] you are accessing the value of my_list that is at 'index' (first index is 0). For example if my_list = [2, 4, 6, 8, 1] then my_list[4] evaluates to 1 because one is at index 4 of my_list, 8 is at index 3, 6 at index 2, 4 at index 1 and 2 at index 0. Now if I have my_list[my_list[4]] it will evaluate to my_list[1] (because my_list[4] is 1) which will evaluate to 4in this case. This means that in the question you asked list[list[4]] --> evaluate the list[4] first than use that as index of the outer list.
15th Mar 2017, 8:39 PM
Ulisses Cruz
Ulisses Cruz - avatar
15th Mar 2017, 8:40 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
list[4] will return the element value, you can use this value to access other elements this way: list[list[4]]
15th Mar 2017, 9:03 PM
Mohamed Salah
Mohamed Salah - avatar
0
Thankyou all alot for your replies, especially Ulisses', You have enlightened me a little but not fully, I will research it, thanks!
3rd Apr 2017, 8:26 PM
Jayelar
Jayelar - avatar
0
(list[list[4]]) start with inner brackets & substitute = (list[5]) = 8
11th Jan 2019, 4:16 AM
Meeboh
Meeboh - 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