I found this code snippet to be very interesting.
a = [0, 1, 2, 3]
for a[-1] in a:
print(a)
Output is as follows:
[0, 1, 2, 0]
[0, 1, 2, 1]
[0, 1, 2, 2]
[0, 1, 2, 2]
I am trying to understand why python does that. Is it because python is trying to re-use the index? For loop somehow slices the list?
We can add or delete an element while iterating the list, but when we are trying to access the variable using index, it gives bizarre output.
Can someone help me understand the interaction between for loop and index in the list? Or simply explain this output?