1

My question is, how do you check if all of the elements of a list of lists are in another list of lists? Or maybe better phrased, how do you check if one list of lists is a subset of another list of lists? The answer posted below only works if you have a list of, say, strings but the answer does not work for my situation.

How to check if all items in a list are there in another list?

Something I have tried is something like this:

if all(item in list1 for item in list2): 

which does not work.

Community
  • 1
  • 1
sfortney
  • 2,075
  • 6
  • 23
  • 43

2 Answers2

5

Convert your sublists to tuples, for example:

In [2]: a = [[2,3],[5,6],[8,9]]

In [3]: b = [[2,3],[5,6],[8,9], [10,11]]

In [4]: set(tuple(x) for x in a).issubset(tuple(x) for x in b)
Out[4]: True
Akavall
  • 82,592
  • 51
  • 207
  • 251
  • 1
    *Note* This only works if all elements (sublists) are unique. – sberry Feb 19 '15 at 01:29
  • 1
    @sberry Why? It looks like it'd work to me. sfortney doesn't specify that counts must be preserved. – Veedrac Feb 19 '15 at 01:36
  • 1
    @Veedrac, I the point that @sberry is makings is: say `a = [5,5]` and `b = [5,3]`, then the above method ignores the fact that `5` shows up twice in `a`. However, `OP` has not been very clear on what constitutes "subset", so I hope my answer is helpful. – Akavall Feb 19 '15 at 01:41
  • @Akavall No you got it exactly right. The only elements I care about for determining a subset are the lists/tuples in the list. The tuple method works great. And the elements by construction are unique so that is not a concern. I think I tried doing something similar with the issubset method before but you cannot take the set of a list of lists so that's where I ran into trouble.. Thanks for the clear and concise answer. :) – sfortney Feb 19 '15 at 04:50
1

This is really really easy if you just even tried to see what that piece of code gave you...

>>> l1 = [1,2]
>>> l2 = [1,2,3,4,5]
>>> all(l1)
True
>>> [item in l1 for item in l2]
[True, True, False, False, False]
>>> help(all)
Help on built-in function all in module builtins:

all(...)
    all(iterable) -> bool

    Return True if bool(x) is True for all values x in the iterable.
    If the iterable is empty, return True.

Anyhow you need to turn them into sets and compute the issubset method

>>> s1 = set(l1)
>>> s2 = set(l2)
>>> s1.issubset(s2)
True

Edit yes, as others have noted this only works if all the elements of the list are unique and you're not looking for a set math but for an exact match since sets are by default a collection of unique objects.

ljetibo
  • 3,048
  • 19
  • 25
  • 1
    I don't think this quite answers the question because the lists you are using are not lists of lists. They are lists of ints. If you were to try this with lists of lists it would not work. I could be wrong, and I am not near my machine to test this but I think that is correct. :) – sfortney Feb 19 '15 at 04:53
  • 1
    @sfortney you are right, sry, didn't read it through I guess. Just saw the expr. and immediately thought you just copy pasted if as part of a HW or an assignment and now got stuck using it. (Since list comparing comes quite often, and `all` gets abused everytime) Sorry. – ljetibo Feb 19 '15 at 09:27
  • @sfortney thanks, I was worried you were mad because I was snippy. – ljetibo Feb 19 '15 at 17:06