DEV Community

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

Posted on • Edited on

Set in Python (3)

Buy Me a Coffee

*Memos:

  • My post explains a set and copy.
  • My post explains the useful functions for a set (1).

difference() can return the zero or more elements which the set has but *others don't have as shown below:

*Memos:

  • The 1st or the later arguments are *others(Optional-Type:iterable) for zero or more iterables.
  • Don't use any keywords like *others=, others=, others=, etc.
  • You can use - with sets instead of difference().
  • difference() creates a copy while difference_update() doesn't.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {10, 20}

print(A.difference(B)) # {40, 20}
print(A.difference(C)) # {40, 30}
print(A.difference(B, C)) # {40}

print(A - B) # {40, 20}
print(A - C) # {40, 30}
print(A - B - C) # {40}
Enter fullscreen mode Exit fullscreen mode

difference_update() can return the zero or more elements which the set has but *others don't have as shown below:

*Memos:

  • The 1st or the later arguments are *others(Optional-Type:iterable) for zero or more iterables.
  • Don't use any keywords like *others=, others=, others=, etc.
  • You can use -= and | with sets instead of difference_update().
  • difference_update() doesn't create a copy while difference() does.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {10, 20}

A.difference_update(B)
# A -= B
print(A) # {20, 40}

A.difference_update(C)
# A -= C
print(A) # {40}

A.difference_update(B, C)
# A -= B | C
print(A) # {40}
Enter fullscreen mode Exit fullscreen mode

symmetric_difference() can return the zero or more elements which the set has but *other doesn't have or which the set doesn't have but *other has as shown below:

*Memos:

  • The 1st argument is *other(Optional-Type:iterable) for zero or one iterable.
  • Don't use any keywords like *other=, other=, other=, etc.
  • You can use ^ with sets instead of symmetric_difference().
  • symmetric_difference() creates a copy while symmetric_difference_update() doesn't.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {10, 20}

print(A.symmetric_difference(B)) # {40, 50, 20}
print(A.symmetric_difference(C)) # {40, 30}

print(A ^ B) # {40, 50, 20}
print(A ^ C) # {40, 30}
Enter fullscreen mode Exit fullscreen mode

symmetric_difference_update() can return the zero or more elements which the set has but *other doesn't have or which the set doesn't have but *other has as shown below:

*Memos:

  • The 1st argument is *other(Optional-Type:iterable) for zero or more iterable.
  • Don't use any keywords like *other=, other=, other=, etc.
  • You can use ^= with sets instead of symmetric_difference_update().
  • symmetric_difference_update() doesn't create a copy while symmetric_difference() does.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {10, 20}

A.symmetric_difference_update(B)
# A ^= B
print(A) # {40, 50, 20}

A.symmetric_difference_update(C)
# A ^= C
print(A) # {40, 10, 50}
Enter fullscreen mode Exit fullscreen mode

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

*Memos:

  • The 1st argument is *other(Optional-Type:iterable) for an iterable.
  • Don't use any keywords like *other=, other=, other=, etc.
A = {10, 20, 30}
B = {40, 50}
C = {30, 40}

print(A.isdisjoint(B)) # True
print(A.isdisjoint(C)) # False
Enter fullscreen mode Exit fullscreen mode

You can use issubset() and issuperset() as shown below:

*Memos:

  • issubset() can check if every element in the set is in *other:
  • issuperset() can check if every element in *other is in the set: *Memos:
    • The 1st argument is *other(Optional-Type:iterable) for an iterable.
    • Don't use any keywords like *other=, other=, other=, etc.
  • You can use <= and >= with sets instead of issubset() and issuperset() respectively.
A = {10, 20, 30}
B = {10, 20, 30, 40}
C = {10, 20, 30}
D = {10, 20}

print(A.issubset(B)) # True
print(A.issubset(C)) # True
print(A.issubset(D)) # False
print(A <= B) # True
print(A <= C) # True
print(A <= D) # False

print(A.issuperset(B)) # False
print(A.issuperset(C)) # True
print(A.issuperset(D)) # True
print(A >= B) # False
print(A >= C) # True
print(A >= D) # True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)