Python: See if one set contains another entirely?

PythonSet

Python Problem Overview


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

Python Solutions


Solution 1 - Python

Those are lists, but if you really mean sets you can use the issubset method.

>>> s = set([1,2,3])
>>> t = set([1,2])
>>> t.issubset(s)
True
>>> s.issuperset(t)
True

For a list, you will not be able to do better than checking each element.

Solution 2 - Python

For completeness: this is equivalent to issubset (although arguably a bit less explicit/readable):

>>> set([1,2,3]) >= set([2,1])
True
>>> set([1,2,3]) >= set([3,5,9])
False

Solution 3 - Python

You can use either set.issubset() or set.issuperset() (or their operator based counterparts: <= and >=). Note that the methods will accept any iterable as an argument, not just a set:

>>> {1, 2}.issubset([1, 2, 3])
True
>>> {1, 2, 3}.issuperset([1, 2])
True

However, if you use operators, both arguments must be sets:

>>> {1, 2} <= {1, 2, 3}
True
>>> {1, 2, 3} >= {1, 2}
True

Solution 4 - Python

If you suspect a set to be a subset of another, and intersect those two sets together, the result is equal to itself if it is a subset.

a = [2,1,3,3]
b = [5,4,3,2,1]
set(a).intersection(set(b)) == set(a)
>>True

Solution 5 - Python

One option is left untouched -- subtraction:

>>> {1, 2} - {1, 2, 3}
set([])
>>> {1, 2, 3} - {1, 2}
set([3])

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:

>>> def check_contains(a, b):
...     diff = a - b
...     if not diff:
...         # All elements from a are present in b
...         return True
...     print('Some elements are missing: {}'.format(diff))
...     return False
...
>>> check_contains({1, 2}, {1, 2, 3})
True
>>> check_contains({1, 2, 3}, {1, 2})
Some elements are missing: set([3])
False

Solution 6 - Python

>>> set([1,2,3]).issuperset(set([2,1]))
True
>>> set([1,2,3]).issuperset(set([3,5,9]))
False

Solution 7 - Python

Below function return 0 if mainlist doesn't contains sublist fully and 1 if contains fully.

def islistsubset(sublist,mainlist):
     for item in sublist:
             if item in mainlist:
                     contains = 1
             else:
                     contains = 0
                     break;
     return contains

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNick HeinerView Question on Stackoverflow
Solution 1 - PythondanbenView Answer on Stackoverflow
Solution 2 - PythonChristopheDView Answer on Stackoverflow
Solution 3 - PythonEugene YarmashView Answer on Stackoverflow
Solution 4 - PythonJordan StefanelliView Answer on Stackoverflow
Solution 5 - PythonArtem SkoretskiyView Answer on Stackoverflow
Solution 6 - PythonMamataView Answer on Stackoverflow
Solution 7 - PythonBobin Motti ThomasView Answer on Stackoverflow