Simple as:
for (i, tup) in enumerate(somelist):
if determine(tup):
del somelist[i]
This deletes the elements in really in place. I have cases where I have lots of codes inside the for and deleting is just one possible action, usually not the main one and on a except
clause, where frequently the list is too big to make copies.
This code running:
>>> somelist = list(enumerate( 'Sun Mon Tue Wed Thu Fri Sat'.split(), 1))
>>> print somelist
[(1, 'Sun'), (2, 'Mon'), (3, 'Tue'), (4, 'Wed'), (5, 'Thu'), (6, 'Fri'), (7, 'Sat')]
>>> def determine(tup):
... return tup[0] == 4 or tup[1] == 'Fri'
...
>>> for (i, tup) in enumerate(somelist):
... if determine(tup):
... del somelist[i]
...
>>> print somelist
[(1, 'Sun'), (2, 'Mon'), (3, 'Tue'), (5, 'Thu'), (7, 'Sat')]