J
Jeremy Bowers
In this case then, shouldn't my 'except Exception' raise an error or
warning like:
"Hey, stupid, you can't iterate and object and change it at the same time!"
But you can do that, so why should it?
Certain operations are dangerous, but, well, it's impossible to enumerate
all the dangerous things and it's better to not give people false
assurances when they find one of them that slipped by.
One idiom that does what you want, though it doesn't always work for all
situations, is to iterate over the list backwards. If you're only removing
a few items from a short list, that can be faster than building a new one.
However, removing items gets to be n^2 pretty fast, so it can be better to
build a new list, which the list comprehensions make easy:
newList = [x for x in oldList if myCondition(x)]