Avoid adding to or removing from a collection, such as a list, as you iterate that collection in a for loop:
data = [1, 2, 3, 4]
for item in data:
    data.remove(item)
print(data)  # [2, 4] <-- every OTHER item was removed!In the example above, 1 is removed, shifting 2 to index 0. The loop then moves to index 1, removing 3 (and skipping 2!).
You can avoid this pitfall by: - using a list comprehension to produce a new list (as a way of filtering items):
data = [x for x in data if x % 2 == 0]while loop and .pop() (treating the list as a stack):
  while data:
    item = data.pop()