The issubset method
The issubset
method checks the occurrence of set elements in a sequence, which can be another set, a list, a tuple. The method returns a Boolean value - True
or False
. In the parameter, we specify the sequence with which we want to compare the set elements.
Syntax
set.issubset(sequence)
Example
Let's check the inclusion of elements of the first set in the second using the issubset
method:
st1 = {'a', 'b', 'c'}
st2 = {'e', 'g', 'a'}
res = st1.issubset(st2)
print(res)
Result of code execution:
False
Example
Now let's check the inclusion of elements again using the issubset
method:
st1 = {'a', 'b', 'c'}
st2 = {'a', 'b', 'c'}
res = st1.issubset(st2)
print(res)
Result of code execution:
True
Example
Let's compare the elements of a set with the elements of a list:
st = {'a', 'b', 'c'}
lst = ['a', 'b', 'c']
res = st.issubset(lst)
print(res)
Result of code execution:
True
Example
Now let's compare the elements of a set with a tuple:
st = {'a', 'b', 'c'}
tlp = ('a', 'b', 'c')
res = st.issubset(tlp)
print(res)
Result of code execution:
True
Example
The issubset
method also has a short form:
st1 = {'a', 'b', 'c'}
st2 = {'a', 'b', 'c'}
res = st1 <= st2
print(res)
Result of code execution:
True
See also
-
method
intersection
,
which returns the intersection of sets -
method
difference
,
which returns the differences between sets -
method
issuperset
,
which checks the presence of elements of a sequence in a set -
method
symmetric_difference
,
which excludes common elements of a set and a sequence