Executing the following sample code to remove elements from list:
l = ['A', 'B', 'C', 'D']
for x in l:
    print(x, l)
    if x == 'A' or x == 'B':
        l.remove(x)
print(l)
The output in both Python 2.x and Python 3.x is:
$ python3 test.py                                                                                      
A ['A', 'B', 'C', 'D']
C ['B', 'C', 'D']
D ['B', 'C', 'D']
['B', 'C', 'D']
The expected output should be:
['C', 'D']
Why is Python behaving this way ? What is the safest way to remove elements from a list ?
 
    