+ 2

Why this output occur?

x=[1,2,3,4] for i in x: x.remove(i) print(x) #output=[2,4] but why can anyone explain it.

22nd May 2018, 4:15 PM
Maninder $ingh
Maninder $ingh - avatar
3 odpowiedzi
+ 2
In first iteration i=1 and the x becomes x=[2,3,4].Now i moves to next index that is i=3 and 3 is removed. Now the x size is 2, next i looks for next 3rd index, it is not possible so x has 2 and 4.
22nd May 2018, 4:35 PM
G.S.N.V. Suraj
G.S.N.V. Suraj - avatar
+ 2
22nd May 2018, 4:57 PM
Maninder $ingh
Maninder $ingh - avatar
0
For each iteration i takes the value of next index in x. When 1 is removed the index of 2 is one(first position) but i will take the element at second position so i!=2.
22nd May 2018, 6:10 PM
G.S.N.V. Suraj
G.S.N.V. Suraj - avatar