I need to delete elements from a list whilst iterating over it (I cannot create a new list as it is referenced in code I have no control over).
The best I've come up with is:
last_index = len(somelist) - 1
for (index,item) in enumerate(reversed(somelist)):
if somecondition(item):
del somelist[last_index - index]
Are there better alternatives? I've seen this post and this one too, but none of the solutions provided are as efficient or as concise (IMHO).