how to check if a set is a subset of another set in python

To check if a set is a subset of another set in Python, you can use the issubset() method of sets. Here is an example:

main.py
set1 = {1, 2, 3, 4, 5}
set2 = {1, 3, 5}
set3 = {1, 3, 6}

print(set2.issubset(set1))  # True
print(set3.issubset(set1))  # False
129 chars
7 lines

In this example, set1 contains the elements 1, 2, 3, 4, and 5. set2 contains a subset of set1, namely 1, 3, and 5, so set2.issubset(set1) returns True. On the other hand, set3 contains an element (6) that is not in set1, so set3.issubset(set1) returns False.

related categories

gistlibby LogSnag