Search code examples
pythonlistcomparison-operators

How to check if a 3 leveled nested list is a subset of another 3 leveled nested list


I have 2 lists as list<list<list>> lists where I want to check if one of them is a subset of the other.

list_1 = [
           [
             [1,2],[2,3]
           ],
           [
             [3,4],[5,6]
           ]
         ]
list_2 = [
           [
             [3,4], [5, 6]
           ]
         ]

So expected output is that as list2 has [[[3,4]]] which is a part of list_1 so it should be a subset.

list_1 has 2 elements and its second element matches the first element in list_2 so list_2 is a subset of list_1.

The comparison is not at the element level but at list level.

I tried set(list_2) < set(list_1) but results in unhashable type: list. How can I achieve the above comparison then?


Solution

  • You can check that all sublist in list_2 are within list_1:

    all(x in list_1 for x in list_2)
    True
    

    Here you have the live example