0

About remove() in python

I have dought about the following code a=[1,2,3,4,5] for n in a: a.remove(n) print(a) the output is [2,4], why not it is [ ]

16th Oct 2018, 3:22 AM
Rakesh Kumar N
Rakesh Kumar N - avatar
2 Respostas
+ 6
The list is updated dynamically while you iterate over it. First you remove the first element (1). The updated list looks like this: [2,3,4,5]. Then you remove the second element which is now 3 and the list looks like this: [2,4,5]. Then you remove the third element (5) and the list looks like this: [2,4].
16th Oct 2018, 5:16 AM
Anna
Anna - avatar
+ 1
that's very informative thank you
16th Oct 2018, 7:48 AM
Rakesh Kumar N
Rakesh Kumar N - avatar