Nikhil Prabhu
Nikhil Prabhu

Reputation: 1132

Checking if a list of tuples is a subset of another

Firstly, note that I have gone through the other list-subset questions and they are not related to my problem here.

I have two lists

>>> l1 = [[(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked')]]
>>>
>>> l2 = [(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked'), (9, 8, 'CC', 'cc', -1, 'and', 'and'), (10, 7, 'JJ', 'xcomp', -1, 'angry', 'angry')]

I'm trying to check if one is a subset of another.

But before that I checked out the results of subtracting one list from another and I got disappointing results -

>>> [word for word in l1 if word not in l2]
[[(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked')]]

>>> [word for word in l2 if word not in l1]
[(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked'), (9, 8, 'CC', 'cc', -1, 'and', 'and'), (10, 7, 'JJ', 'xcomp', -1, 'angry', 'angry')]

Why am I getting identical lists as my results? Does this have something to do with the fact that they are tuples?

Upvotes: 2

Views: 3178

Answers (3)

mitoRibo
mitoRibo

Reputation: 4548

The problem is that l1 is a list of list of tuple (i.e. [[tuple]]), whereas l2 is a list of tuple (i.e. [tuple]). If you change this the output from the list comprehensions are what you expect:

l1 = [(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked')]
l2 = [(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked'), (9, 8, 'CC', 'cc', -1, 'and', 'and'), (10, 7, 'JJ', 'xcomp', -1, 'angry', 'angry')]
​
a = [word for word in l1 if word not in l2]
b = [word for word in l2 if word not in l1]
​
print a
print b

[]
[(9, 8, 'CC', 'cc', -1, 'and', 'and'), (10, 7, 'JJ', 'xcomp', -1, 'angry', 'angry')]

Upvotes: 5

SparkAndShine
SparkAndShine

Reputation: 18045

Use issubset,

l1 = [(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked')]
l2 = [(7, -1, 'VBD', 'null', -1, 'looked', 'looked'), (8, 7, 'JJ', 'xcomp', -1, 'shocked', 'shocked'), (9, 8, 'CC', 'cc', -1, 'and', 'and'), (10, 7, 'JJ', 'xcomp', -1, 'angry', 'angry')]

>>> set(l1).issubset(l2)
True

Upvotes: 4

joaquinlpereyra
joaquinlpereyra

Reputation: 966

You can just use sets and the <= operator (is subset of).

>>> l1 = [2,4,6,8]
>>> l2 = [2,4,6,8,10]
>>> set(l1) <= set(l2)
True

Upvotes: 0

Related Questions