0
How is this code working?
a = [0,1,2,3] for a[-1] in a: print (a[-1]) #output:[0,1,2,2] #a becomes [0,1,2,2] Why does a change.?
2 Answers
+ 4
This is a bad practice only for brain teaser.
Consider it as for item in a
the value of item changes from 0, 1, 2 to 3 in each iteration.
Now the indicator becomes a[-1] (last element in the list a).
So in the first three iterations, a[-1] changes from 0, 1 to 2.
And in the fourth iteration, as its value is now 2, the last output is 2.