Is there a fast way to check if one set entirely contains another?
Something like:
>>>[1, 2, 3].containsAll([2, 1])
True
>>>[1, 2, 3].containsAll([3, 5, 9])
False
Is there a fast way to check if one set entirely contains another?
Something like:
>>>[1, 2, 3].containsAll([2, 1])
True
>>>[1, 2, 3].containsAll([3, 5, 9])
False
Below function return 0 if mainlist doesn't contains sublist fully and 1 if contains fully.
You can use either
set.issubset()
orset.issuperset()
(or their operator based counterparts:<=
and>=
). Note that the methods will accept any iterable as an argument, not just a set:However, if you use operators, both arguments must be sets:
One option is left untouched -- subtraction:
Basically you check what elements in first list are not in second list.
I found it very handy since you could show what values are missing:
For completeness: this is equivalent to
issubset
(although arguably a bit less explicit/readable):Those are lists, but if you really mean sets you can use the issubset method.
For a list, you will not be able to do better than checking each element.