On Fri, 04 Feb 2005 14:49:43 -0500, rbt wrote: > Alan McIntyre wrote: >> I think it's because you're modifying the list as you're iterating over > > 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)] -- http://mail.python.org/mailman/listinfo/python-list