0
Why output is 1,2,3, 4,5,6,7,8,8..Why there are two 8's?
3 Answers
+ 3
You are using k in first loop and after loop k=9.
You are modifying a[k]=a[9] by second loop by taking value into from list a.
So a[0] is modifying in each iteration from 0 to 8 and printing so last printing a[9] has value 8 modified just before printing...
Hope it helps..
0
I got it
a = [0, 1, 2, 3]
for a[-1] in a:
print(a)
[0, 1, 2, 0] # a[-1] == a[0]
[0, 1, 2, 1] # a[-1] == a[1]
[0, 1, 2, 2] # a[-1] == a[2]
[0, 1, 2, 2] # a[-1] == a[3]