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 [ ]
2 Antworten
+ 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].
+ 1
that's very informative thank you