Jordan Violet
Jordan Violet

Reputation: 73

Most efficient way to intersect list of lists in Python?

I asked a previous question about reading in CSV's and using Pandas and, while that got me closer to my answer, it wasn't quite what I was looking for.

I have two lists:

a1 = [['1', 'foo'], ['2', 'bar'], ['3', 'Foo']]
a2 = [['1', 'foo'], ['2', 'bar'], ['3', 'Foo'], ['4', 'Bar']]

I am looking to retrieve these things:

I got the data out of my CSV's that I wanted using Pandas, and now I have them in the list format shown above. Thanks in advance!

Upvotes: 1

Views: 515

Answers (2)

Joran Beasley
Joran Beasley

Reputation: 113930

the easiest solution is to convert it to sets

a1 = [['1', 'foo'], ['2', 'bar'], ['3', 'Foo']]
a2 = [['1', 'foO'], ['2', 'bar'], ['3', 'Foo'], ['4', 'Bar']]

a1_set = set([v[0] for v in a1])
a2_set = set([v[0] for v in a2])

print("Items in a1,but not a2:",[x for x in a1 if x[0] in a1_set-a2_set])
print("Items in a2,but not a1:",[x for x in a2 if x[0] in a2_set-a1_set])

d1 = dict(a1)
d2 = dict(a2)
items_in_both = [(k,(d1[k],d2[k]) if d1[k] != d2[k] else d1[k]) for k in a2_set&a1_set]

print("Items in both sets:", items_in_both)

Upvotes: 2

Ali Sarchami
Ali Sarchami

Reputation: 1187

So, basically you want to find elements which are in one list and not in another:

[x for x in a2 if x not in a1]

and if you want common elements you can use:

[x for x in a2 if x in a1]

Upvotes: 0

Related Questions