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

pythonset.issubset(sequence)

Example

Let's check the inclusion of elements of the first set in the second using the issubset method:

pythonst1 = {'a', 'b', 'c'}
st2 = {'e', 'g', 'a'}
res = st1.issubset(st2)
print(res)

Result of code execution:

pythonFalse

Example

Now let's check the inclusion of elements again using the issubset method:

pythonst1 = {'a', 'b', 'c'}
st2 = {'a', 'b', 'c'}
res = st1.issubset(st2)
print(res)

Result of code execution:

pythonTrue

Example

Let's compare the elements of a set with the elements of a list:

pythonst = {'a', 'b', 'c'}
lst = ['a', 'b', 'c']
res = st.issubset(lst)
print(res)

Result of code execution:

pythonTrue

Example

Now let's compare the elements of a set with a tuple:

pythonst = {'a', 'b', 'c'}
tlp = ('a', 'b', 'c')
res = st.issubset(tlp)
print(res)

Result of code execution:

pythonTrue

Example

The issubset method also has a short form:

pythonst1 = {'a', 'b', 'c'}
st2 = {'a', 'b', 'c'}
res = st1 <= st2
print(res)

Result of code execution:

pythonTrue

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