How can I fix this statement:
for i in LISTA and i not in LISTB:
print i
How can I fix this statement:
for i in LISTA and i not in LISTB:
print i
new_list = set(LISTA) - set(LISTB) # if you don't have duplicate
for i in new_list:
print i
Or :
for i in LISTA:
if i in LISTB:
continue
print i
A more sophisticated solution. This is a simple intersection complement.
a = set([1, 2, 3])
b = set([3, 4, 5])
print(a - b)
for i in (i for i in LISTA if i not in LISTB):
print i
The part in parentheses is a generator expression. The benefit of this over other methods that is that it doesn't create duplicate (temporary) sets or list objects. This is especially important if LISTA and/or LISTB are really large.