2. set Methods

2.1. set.isdisjoint()

  • Return True if the set has no elements in common with other

  • Sets are disjoint if and only if their intersection is the empty set.

{1,2}.isdisjoint({3,4})     # True

2.2. set.issubset()

  • Test whether every element in the set is in other

{1,2} <= {3,4}              # False
{1,2} <= {1,2}              # True
{1,2} <= {1,2,3}            # True
{1,2} < {1,2,3}             # True
{1,2} < {1,2}               # False

2.3. set.issuperset()

  • Test whether every element in other is in the set

{1,2} > {1,2,3}             # False
{1,2} > {1,2}               # False
{1,2,3} > {1,2}             # True
{1,2} >= {1,2}              # True
{1,2,3} >= {1,2}            # True

2.4. set.union()

  • Return a new set with elements from the set and all others

{1,2} | {1,2}               # {1, 2}
{1,2,3} | {1,2}             # {1, 2, 3}
{1,2,3} | {1,2,4}           # {1, 2, 3, 4}
{1,2} | {1,3} | {2,4}       # {1, 2, 3, 4}

2.5. set.intersection()

  • Return a new set with elements common to the set and all others

{1,2} & {2,3}               # {2}
{1,2} & {2,3} & {2,4}       # {2}
{1,2} & {2,3} & {3}         # set()

2.6. set.difference()

  • Return a new set with elements in the set that are not in the others

{1,2} - {2,3}               # {1}
{1,2} - {2,3} - {3}         # {1}
{1,2} - {1,2,3}             # set()

2.7. set.symmetric_difference()

  • Return a new set with elements in either the set or other but not both

{1,2} ^ {1,2}               # set()
{1,2} ^ {2,3}               # {1, 3}
{1,2} ^ {1,3}               # {2, 3}