On Thu, 17 Feb 2005 10:22:01 -0800, James Stroud wrote: > It seems I need constructs like this all of the time > > i = 0 > while i < len(somelist): > if oughta_pop_it(somelist[i]): > somelist.pop(i) > else: > i += 1 > > There has to be a better way... > > Any thoughts?
Others have pointed out somelist = [x for x in somelist if not oughta_pop_it(x)] but I'd also point out what I haven't seen, that your algorithm is O(n^2) making fairly reasonable assumptions, whereas the list comprehension is O(n). For long lists, that adds up fast. In fact, if you pop two things off in the first half of the list, you've already lost and everything after that is a waste. The only time you want to do that is when you're memory-constrained, since it will take less memory... and I gotta say, I don't think Python is the best "memory-constrained" language in the world :-) Besides, if you're really dealing with that much data, it's probably time to use a database of some sort and tell *it* what things to eliminate. -- http://mail.python.org/mailman/listinfo/python-list