Iterating over a list, possibly deleting elements. It's the bane of my existence. Suppose, for instance, we have a list of numbers, and want to iterate over it and delete each and every odd one. Sounds simple, right?
Sorta. It's trivial, but the code you end up with is pretty gruesome.
>>> nums = [1.0,2.0,3.0,4.0,5.0]
>>> for n in range(len(nums)-1,-1,-1):
# Same as saying "if nums[n] is odd"
if nums[n] % 2 != 0:
del nums[n]
>>> print nums
[2.0, 4.0]
Surely there's a cleaner way to do that loop? Anyone?