user971584
user971584

Reputation: 55

How do I check to see if all of the items in one list are in a second list in python?

I'm new to python and have put this together from a short class on Python and some Googling. I'm trying to compare two lists of strings to see if all of the items of List A are in List B. If any items are not in List B, I want it to print a notification message.

List_A = ["test_1", "test_2", "test_3", "test_4", "test_5"]
List_B = ["test_1", "test_2", "test_3", "test_4"]

Code:

for item in List_A:
     match = any(('[%s]'%item) in b for b in List_B)
     print "%10s %s" % (item, "Exists" if match else "No Match in List B")

Output:

test_1 No Match in List B

test_2 No Match in List B

test_3 No Match in List B

test_4 No Match in List B

test_5 No Match in List B

The first four should match but do not, and the fifth one is correct. I have no idea why it isn't working. Can someone tell me what I'm doing wrong? Any help would be greatly appreciated.

Upvotes: 1

Views: 180

Answers (4)

Kuldeep Rishi
Kuldeep Rishi

Reputation: 470

List_A = ["test_1", "test_2", "test_3", "test_4", "test_5"]
List_B = ["test_1", "test_2", "test_3", "test_4"]

for item in List_A:
     match = any((str(item)) in b for b in List_B)
     print "%10s %s" % (item, "Exists" if match else "No Match in List B")

will output

test_1 Exists
test_2 Exists
test_3 Exists
test_4 Exists
test_5 No Match in List B

Upvotes: 0

Elazar
Elazar

Reputation: 21615

>>> '[%s]'%"test_1"
'[test_1]'

You are checking if "[test_1]" is a substring of some string in list_B, and so forth.

This should work:

for item in List_A:
     match = any(item in b for b in List_B)
     print "%10s %s" % (item, "Exists" if match else "No Match in List B")

But since you are not really looking for substrings, you should test in and nothing more:

for item in List_A:
     match = item in List_B
     print "%10s %s" % (item, "Exists" if match else "No Match in List B")

but you can simply use set difference:

print set(List_A) - set(List_B)

Upvotes: 5

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250961

You can convert List_B into a set, as sets provide an O(1) lookup:

Though note that sets allow you to store only hashable(immutable) items. If List_B contains mutable items then convert them to immutable items first, If that is not possible then sort List_B and use bisect module to do binary search on sorted List_B.

List_A = ["test_1", "test_2", "test_3", "test_4", "test_5"]
List_B = ["test_1", "test_2", "test_3", "test_4"]
s = set(List_B)
for item in List_A:
    match = item in s  # check if item is in set s
    print "%10s %s" % (item, "Exists" if match else "No Match in List B")

output:

test_1 Exists
test_2 Exists
test_3 Exists
test_4 Exists
test_5 No Match in List B

Upvotes: 0

TerryA
TerryA

Reputation: 59974

Just use a simple for-loop:

List_A = ["test_1", "test_2", "test_3", "test_4", "test_5"]
List_B = ["test_1", "test_2", "test_3", "test_4"]
for i in List_A:
    print "{:>10} {}".format(i, "Exists" if i in List_B else "No Match in List B")

Upvotes: 0

Related Questions