Find Element Not in List
Table of Contents
//Find Element Not In List #
Today, I have to write a Python program that prints the elements of list A that are not in list B as a list.
For example, if list A = [1, 2, 3] and list B = [1, 2], then the output will be [3].
My solution is to go through all elements in list A and for each element I check if it exist in list B. If it doesn’t I will put it all in the output list.
HERE IS THE CODE:
a = [1,2]
b = [1, 2]
output = []
for i in a:
#print(i)
if i not in b:
output.append(i)
print(output)