Python Set issubset() Method

The set.issubset() method returns true if the set (on which the issubset() is called) contains every element of the other set passed as an argument.

Syntax:

set.issubset(other_set)

Parameters:

other_set: (Required) A set.

Return Value:

Returns True if set is a subset of the argument other_set.

The following example demonstrates the set.issubset() method.

Example: issubset()
nums = {1, 2, 3, 4, 5 }
oddNums = {1, 3, 5}
primeNums = {1, 3, 5, 7}

print(oddNums.issubset(nums))
print(primeNums.issubset(nums))
Output
True
False

In the above example, nums set contains all the elements of oddNums and so oddNums is a subset of nums. In the same way, nums does not contain all the elements of primeNums, and so, primeNums is not a subset of nums.

The set.issubset() method can take other iterable types as an arguement such as list, string, dictionary, and tuple.

Example: issubset() on Iterables
char_set = {'a','b','c'}
char_list = ['a','b','c','d']
char_str = 'ghij'
char_dict = {'a':1,'b':2}
char_tuple = ('a', 'b', 'c', 'd')

print(char_set.issubset(char_list))
print(char_set.issubset(char_str))
print(char_set.issubset(char_dict))
print(char_set.issubset(char_tuple))
Output
True 
False 
False 
True
Want to check how much you know Python?