DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Set & Frozenset functions in Python (6)

Buy Me a Coffee

*Memo:

  • My post explains set and frozenset functions (1).
  • My post explains set and frozenset functions (2).
  • My post explains set and frozenset functions (3).
  • My post explains set and frozenset functions (4).
  • My post explains set and frozenset functions (5).
  • My post explains set and frozenset functions (7).
  • My post explains a set (1).
  • My post explains a frozenset (1).

issubset() can check if all the elements in the set or frozenset are in other as shown below:

*Memo:

  • The 1st argument is other(Required-Type:Iterable):
    • Don't use other=.
  • <= can do issubset(), supporting set and frozenset.

<Set>:

A = {10, 20, 30}

print(A.issubset({10, 20, 30}))     # True
print(A.issubset({10, 20, 40}))     # False
print(A.issubset({10, 20}))         # False
print(A.issubset({10, 40}))         # False
print(A.issubset({10, 20, 30, 40})) # True
print(A.issubset({10, 20, 40, 50})) # False
print(A.issubset(set()))            # False
Enter fullscreen mode Exit fullscreen mode
A = {10, 20, 30}

print(A <= {10, 20, 30})     # True
print(A <= {10, 20, 40})     # False
print(A <= {10, 20})         # False
print(A <= {10, 40})         # False
print(A <= {10, 20, 30, 40}) # True
print(A <= {10, 20, 40, 50}) # False
print(A <= set())            # False
Enter fullscreen mode Exit fullscreen mode

<Frozenset>:

A = frozenset([10, 20, 30])

print(A.issubset(frozenset([10, 20, 30])))     # True
print(A.issubset(frozenset([10, 20, 40])))     # False
print(A.issubset(frozenset([10, 20])))         # False
print(A.issubset(frozenset([10, 40])))         # False
print(A.issubset(frozenset([10, 20, 30, 40]))) # True
print(A.issubset(frozenset([10, 20, 40, 50]))) # False
print(A.issubset(frozenset([])))               # False
Enter fullscreen mode Exit fullscreen mode
A = frozenset([10, 20, 30])

print(A <= frozenset([10, 20, 30]))     # True
print(A <= frozenset([10, 20, 40]))     # False
print(A <= frozenset([10, 20]))         # False
print(A <= frozenset([10, 40]))         # False
print(A <= frozenset([10, 20, 30, 40])) # True
print(A <= frozenset([10, 20, 40, 50])) # False
print(A <= frozenset([]))               # False
Enter fullscreen mode Exit fullscreen mode

issuperset() can check if all the elements in other are in the set or frozenset as shown below:

*Memo:

  • The 1st argument is other(Optional-Type:Iterable):
    • Don't use other=.
  • >= can do issuperset(), supporting set and frozenset.

<Set>:

A = {10, 20, 30}

print(A.issuperset({10, 20, 30}))     # True
print(A.issuperset({10, 20, 40}))     # False
print(A.issuperset({10, 20}))         # True
print(A.issuperset({10, 40}))         # False
print(A.issuperset({10, 20, 30, 40})) # False
print(A.issuperset({10, 20, 40, 50})) # False
print(A.issuperset(set()))            # True
Enter fullscreen mode Exit fullscreen mode
A = {10, 20, 30}

print(A >= {10, 20, 30})     # True
print(A >= {10, 20, 40})     # False
print(A >= {10, 20})         # True
print(A >= {10, 40})         # False
print(A >= {10, 20, 30, 40}) # False
print(A >= {10, 20, 40, 50}) # False
print(A >= set())            # True
Enter fullscreen mode Exit fullscreen mode

<Frozenset>:

A = frozenset([10, 20, 30])

print(A.issuperset(frozenset([10, 20, 30])))     # True
print(A.issuperset(frozenset([10, 20, 40])))     # False
print(A.issuperset(frozenset([10, 20])))         # True
print(A.issuperset(frozenset([10, 40])))         # False
print(A.issuperset(frozenset([10, 20, 30, 40]))) # False
print(A.issuperset(frozenset([10, 20, 40, 50]))) # False
print(A.issuperset(frozenset([])))               # True
Enter fullscreen mode Exit fullscreen mode
A = frozenset([10, 20, 30])

print(A >= frozenset([10, 20, 30]))     # True
print(A >= frozenset([10, 20, 40]))     # False
print(A >= frozenset([10, 20]))         # True
print(A >= frozenset([10, 40]))         # False
print(A >= frozenset([10, 20, 30, 40])) # False
print(A >= frozenset([10, 20, 40, 50])) # False
print(A >= frozenset([]))               # True
Enter fullscreen mode Exit fullscreen mode

isdisjoint() can check if the set or frozenset and other don't have their common elements as shown below:

*Memo:

  • The 1st argument is other(Required-Type:Iterable):
    • Don't use other=.
  • not keyword and & can do isdisjoint(), supporting set and frozenset but it's inefficient.

<Set>:

A = {10, 20, 30}

print(A.isdisjoint({40, 50})) # True
print(A.isdisjoint({20, 40})) # False
print(A.isdisjoint(set()))    # True
Enter fullscreen mode Exit fullscreen mode
A = {10, 20, 30}

print(not (A & {40, 50})) # True
print(not (A & {20, 40})) # False
print(not (A & set()))    # True
Enter fullscreen mode Exit fullscreen mode

<Frozenset>:

A = frozenset([10, 20, 30])

print(A.isdisjoint(frozenset([40, 50]))) # True
print(A.isdisjoint(frozenset([20, 40]))) # False
print(A.isdisjoint(frozenset([])))       # True
Enter fullscreen mode Exit fullscreen mode
A = frozenset([10, 20, 30])

print(not (A & frozenset([40, 50]))) # True
print(not (A & frozenset([20, 40]))) # False
print(not (A & frozenset([])))       # True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)