Introduction to Python Subsets
Set A is a subset of set B if all the elements in A are also in B. Python subset syntax provides several ways to test whether one set is a subset of another. Those include the issubset()
instance method, the issubset()
class method, the subset operator <=
(less than or equal), and the proper subset operator <
(less than). Each of these options provides a slightly different capability and convenience.
Testing for a subset allows you to determine whether one collection of items exists within a larger set. An example of this is a person who has a set of desired items and wants to determine whether all those items are sold by one store. That store will be a one-stop-shop if all the desired items are a subset of the store’s inventory.
Using the issubset() Instance Method
The issubset()
instance method of Python set
objects checks whether all elements of the calling set are included in an iterable, which is given as an argument. It returns True
if they do, and False
otherwise.
Example:
set_a = {1, 2, 3} set_b = {1, 2, 3, 4, 5} result = set_a.issubset(set_b) print(result)
Output:
True
In the example above, set_a
is a subset of set_b
since all elements of set_a
are present in set_b
, resulting in True
.
Example:
set_a = {1, 2, 3} set_b = {2 ,3, 4} result = set_a.issubset(set_b) print(result)
Output:
False
Here, set_a
is not a subset of set_b
because 1
is in set_a
but not in set_b
, leading to False
.
The next example demonstrates that a set is a subset of itself since this fits the definition of a subset.
Example:
set_a = {1, 2, 3} result = set_a.issubset(set_a) print(result)
Output:
True
The following example demonstrates that issubset()
works with other iterable types, e.g. a list.
set_a = {1, 2, 3} lst = [1, 2, 2, 3, 4] result = set_a.issubset(lst) print(result)
Output:
True
In the example above, because the argument given to issubset()
is a list and not a set, it can have repeated elements. And regardless of the repeated element, 2, issubset()
works the same way as it does with set arguments. It returns True
since all the elements of set_a
can be found in lst
.
Using the issubset() Class Method
Similar to the instance method, the issubset()
class method allows you to check whether a set, given as the first argument, has all its elements within an iterable, given as the second.
The difference between the class method and instance method version of issubset()
is that the instance method is called on a specific set
object, whereas the class method includes the set
object as its first parameter. Following the first argument, which must be a set, the second argument can be objects of any iterable type.
Since this version of issubset() is a class method of the set
class, you call it with a set.
prefix.
Example:
set_a = {1, 2, 3} lst = [1, 1, 2, 3, 4] result = set.issubset(set_a, lst) print(result)
Output:
True
This achieves the same result as the previous example but using the class method.
Using the Subset Operator <=
The <=
operator checks whether one iterable object on the left is a subset of another iterable one on the right. It returns True
if it is and False
otherwise.
Example:
set_a = {1, 2, 3} set_b = {1, 2, 3, 4, 5} result = set_a <= set_b print(result)
Output:
True
In the example above, set_a
is a subset of set_b
, and therefore the result is True
.
Unlike the issubset()
instance and class methods, <=
does not require a set
object. It will implicitly convert any iterable objects it operates on to sets and treat them as such.
Example:
lst_a = [1, 2, 2, 3] lst_b = [1, 2, 3, 4, 5] result = lst_a <= lst_b print(result)
Output:
True
In the above example, the result is True
because lst_b
contains all the elements in lst_a
at least once. Since both lists are treated as sets by the operator, lst_b
does not need to include 2 twice for lst_a
to be considered its subset.
Using the Proper Subset Operator <
The <
operator is the same as <=
except that it checks whether one set is a proper subset of another. A proper subset means that all elements of the first set are in the second set, but the second set has at least one more element. Or in other words, a proper subset is like a regular subset, but identical sets are not allowed.
Example:
set_a = {1, 2, 3} set_b = {1, 2, 3, 4, 5} result = set_a < set_b print(result)
Output:
True
In the example above, set_a
is a proper subset of set_b
because all its elements are in set_b
and it has fewer elements.
The next example demonstrates how a proper subset differs from a regular subset.
Example:
set_a = {1, 2, 3} result = set_a < set_a print(result)
Output:
False
Here, the result of the <
operator is False
because set_a
is not a proper subset of itself.
Summary & Reference for Python Subset Operations
Python subset operations allow you to determine whether one set is fully contained within another.
The issubset()
instance method checks whether all elements of the calling set are present in another iterable object. It returns True
if they are, False
otherwise.
set_a = {1, 2, 3} lst = [1, 2, 2, 3, 4] result = set_a.issubset(lst) # result --> True
The issubset()
class method, an alternative to the instance method, is called directly on the set
class. Its first argument must be a set, and the second can be an object of any iterable type.
set_a = {1, 2, 3} lst = [1, 2, 2, 3, 4] result = set.issubset(set_a, lst) # --> True
The subset operator <=
checks whether one iterable is a subset of another. It implicitly converts non-set iterables to sets before performing the comparison.
set_a = [1, 2, 3] set_b = [1, 2, 3, 4, 5] result = set_a <= set_b # --> True
The proper subset operator <
is similar to <=
but ensures that the first set is a proper subset of the second, meaning it has fewer elements.
set_a = {1, 2, 3} set_b = {1, 2, 3, 4, 5} result = set_a < set_b # --> True set_a = {1, 2, 3} set_b = {1, 2, 3} result = set_a < set_b # --> False