bill999
bill999

Reputation: 2550

find if any element of list is in another list

I have a master list, A, and secondary list, B. A contains all of the elements of B and also contains additional elements. I want to identify the values of A that do not occur in B. For example:

A = ['two', 'roads', 'diverged', 'in', 'a']
B = ['roads', 'in']

What I want is:

['two', 'diverged', 'a']

If B contained only one element, say roads, I could do something to the effect of:

for i in range(0, len(A)):
    if 'roads' not in A[i]:
        print A[i]

But I am stuck if B has multiple elements, which it does. I found the any() function, but am not sure why this wouldn't work:

for i in range(0, len(A)):
    if any(B) not in A[i]:
        print A[i]

(I looked at some other answers, but couldn't find what I am looking for. This has possibly been asked elsewhere. If so, forgive me for asking it twice.)

Upvotes: 1

Views: 185

Answers (3)

kmkaplan
kmkaplan

Reputation: 18970

While the accepted answer is certainly the best for many purposes its algorithmic complexity is O(N×M) where N and M are the lengths of A and B. In most case (that is when A and B are not overly long) this won't matter. But if you intend to process very long lists you will improve processing time by first converting B to a frozen set:

tmp = frozenset(B)
[i for i in A if i not in tmp]

The resulting complexity is O(N+M).

Upvotes: 1

Happy001
Happy001

Reputation: 6383

Try list comprehension:

[x for x in A if x not in B]

Upvotes: 4

BrenBarn
BrenBarn

Reputation: 251578

[item for item in a if item not in b]

You could also use sets by doing set(a) - set(b), but I believe this will be slightly slower. Also, using sets also remove duplicate values in the result, which may or may not be what you want.

Upvotes: 5

Related Questions