I saw the following code in a coding challenge, I don't know why it returns 3, but check the code and tell me what do u think.
myList = [1,2,3,4]
for i in myList:
i += 1
print(myList[-2])
When I saw the code I said it will print 4 because in the loop we added 1 in all integers in the list, and [-2]
is supposed to give me the second-last value, which is 4 according to what I think.
I know I'm missing something here but I don't know what it is, so if anyone could explain this to me I'll appreciate it. Probably I'm not understanding i
, I'm not sure.