To check if a set contains all the items of another set in Python, you can use the issubset
method.
In this example,
setA
and setB
containing some elements.issubset
method of setA
to check if all elements of setB
are also present in setA
.issubset
method returns a boolean value: True
if setA
is a subset of setB
, and False
otherwise.setA
is a subset of setB
.setA = {1, 2, 3, 4, 5}
setB = {2, 3}
is_subset = setA.issubset(setB)
print(f'Is setA a subset of setB? {is_subset}')
Is setA a subset of setB? False
In this example,
setA
and setB
containing some elements.issuperset
method of setA
to check if all elements of setB
are also present in setA
.issuperset
method returns a boolean value: True
if setA
is a superset of setB
, and False
otherwise.setA
is a superset of setB
.setA = {1, 2, 3, 4, 5}
setB = {2, 3}
is_superset = setA.issuperset(setB)
print(f'Is setA a superset of setB? {is_superset}')
Is setA a superset of setB? True
In this tutorial, we learned How to check if a Set contains all the items of Another Set in Python language with well detailed examples.