i am trying to remove items in a list if the items do not exist in another list but my program does not work the way i have expected
for example:
logically speaking, all the letters other than 'd', 'e', and 'i' shoulde be removed, but the program returns ['b', 'd', 'e', 'g', 'i', 'k']. it seems the program deletes the first item in the list, skip the next item, then repeat.
what went wrong here? is there another way to do it?
for example:
Code:
l = ['a','b','c','d','e','f','g','h', 'i', 'j','k']
l2 = ['d', 'e', 'i'] # l2 could be a list of raw inputs
for x in l:
if x not in r:
l.remove(x)
print l
what went wrong here? is there another way to do it?
Comment