It might work here (not sure), but in general it's not safe to remove from the list we are iterating on (it can cause some elements to be skipped). Better iterate on a copy.