+ 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.
7 Answers
+ 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.
+ 7
Answered here:
https://www.sololearn.com/Discuss/259971/?ref=app
+ 5
list[4] will return the element value, you can use this value to access other elements this way: list[list[4]]
0
Thankyou all alot for your replies, especially Ulisses', You have enlightened me a little but not fully, I will research it, thanks!
0
(list[list[4]])
start with inner brackets & substitute
= (list[5])
= 8
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"..
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]