Reputation: 25
A = ['a','b','c']
B = ['d','b','e']
res = [i for i in A if i in B]
The above code does not work when the no of elements in A are 300000 and in B are 200000.
How can I solve it?
I also tried
res = {i for i in A if i in B}
res = list(res)
But still could not get the result.
Upvotes: 0
Views: 299
Reputation: 762
If preserving order and/or duplicates doesn't matter, then you can use
A = ['a', 'b', 'c']
B = ['d', 'e', 'f']
res = list(set(A) & set(B))
If order and/or duplicates does matter, then you can use
A = ['a', 'b', 'c']
B = ['d', 'e', 'f']
set_b = set(B)
res = [i for i in A if i in set_b]
Upvotes: 3
Reputation: 180441
A = ['a','b','c']
B = ['d','b','e']
set(A).intersection(B)
To get a list returned:
list(set(A).intersection(B))
intersection takes any iterable as an argument so you just need to make A
a set.
Note, the non-operator versions of union(), intersection(), difference(), and symmetric_difference() will accept any iterable as an argument.
Upvotes: 3
Reputation: 601789
You are basically computing the intersection of two sets. Using the set
data type for this will make this efficient:
A = {'a','b','c'}
B = {'d','b','e'}
res = A & B
Upvotes: 1