I have this code:
test = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O"]
for i in test:
if i not in ["C","D"]:
test.remove(i)
print(test)
I was expecting to get ['C','D']
as a result of running the code above, however I am getting this ['B', 'C', 'D', 'F', 'H', 'J', 'L', 'N']
How can I successfully loop through a list of strings and delete the elements I don't need using Python 3?
NOTE: I don't want to use comprehension lists
thanks