switch to list comprehension to avoid deleting an item from a list while iterating