+ 1
Why can't i delete all the numbers < 6??
It's removing 1 and 3 but why it's not removing 2 & 5??? As in the given e.g.. a = [1, 2, 3, 5, 6, 7] for item in a: if item < 6: a.remove(item) #why it is not removing item = 2 & 5 ?? print(a)
2 Réponses
+ 2
That's because lists are mutable and Python avoids automatically making object copies where possible.
And for loop is iterating through a list, that gets removed values over time.
You can fix that by changing the line:
for item in a:
To:
for item in a.copy():
+ 2
Ahh, thanks a laaot mahn..it worked...
But i still can't understand what you're trying to say..can you plz gimme an example?