x =[1,2,3,4,5,6]
y = [1,2,3,4,5]
if x == y:
print("Numbers found")
else:
print("Numbers not found")
I want to print the numbers which are not present in list y.
CodePudding user response:
The fastest way is to transform both in sets and print the difference:
>>> print(set(x).difference(set(y)))
{6}
This code print numbers present in x
but not in y
CodePudding user response:
to get not matches:
def returnNotMatches(a, b):
return [[x for x in a if x not in b], [x for x in b if x not in a]]
or
new_list = list(set(list1).difference(list2))
to get the intersection:
list1 =[1,2,3,4,5,6]
list2 = [1,2,3,4,5]
list1_as_set = set(list1)
intersection = list1_as_set.intersection(list2)
output:
{1, 2, 3, 4, 5}
you can also transfer it to a list:
intersection_as_list = list(intersection)
or:
new_list = list(set(list1).intersection(list2))
CodePudding user response:
You can do like this:
x = [1,2,3,4,5,6]
y = [1,2,3,4,5]
for i in x:
if i not in y:
print(i)
CodePudding user response:
x =[1,2,3,4,5,6]
y = [1,2,3,4,5]
for i in x:
if i in y:
print(f"{i} found")
else:
print(f"{i} not found")
CodePudding user response:
This is the best option in my opinion.
x =[1,2,3,4,5,6]
y = [1,2,3,4,5]
for number in x:
if number not in y:
print(f"{number} not found")