The behavior of a loop is unspecified if the underlying list is modified while the iteration is in progress. Therefore it should be avoided.
A workaround could be to store the items to be removed/added in a separate list and then remove/add them after the iteration is finished.
Non-compliant example:
Compliant example: