Superset and Subsets

tags: #python/documentation/sets

In Python, you can use the concepts of superset and subset to compare relationships between sets.

Subsets

Theory

A set A is considered a subset of another set B if every element of A is also an element of B.

(AB)
set_A = {1, 2, 3}
set_B = {1, 2, 3, 4, 5}

# Check if set_A is a subset of set_B
is_subset = set_A.issubset(set_B)
print(is_subset) 
True

Superset

Theory

A set B is considered a superset of another set A if every element of A is also an element of B.

(AB)
set_A = {1, 2, 3}
set_B = {1, 2, 3, 4, 5}

# Check if set_B is a superset of set_A
is_superset = set_B.issuperset(set_A)
print(is_superset)  
True
Powered by Forestry.md