The reason you get the error message is because you change the length of the list when you delete something!
Example:
first loop: i = 0, length of list will become 1 less because you delete "applepie" (length is now 2) second loop: i = 1, length of list will now become just 1 because we delete "orangepie" last/third loop: i = 2, Now you should see the problem, since i = 2 and the length of the list is only 1 (to clarify only list[0] have something in it!).
Rather use something like:
for item in in list: if "pie" not in item: new list.append(item)
Willy source share