DEV Community

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

Posted on • Edited on

Set in Python (2)

Buy Me a Coffee

*Memo:

  • My post explains a set and the set with copy.
  • My post explains set functions (2).

add() can add a value to the set as shown below:

*Memo:

  • The 1st argument is elem(Required-Type:Any):
    • Don't use elem=.
A = {10, 30, 50}

print(A)
# {10, 50, 30}

A.add(20)
print(A)
# {10, 50, 20, 30}

A.add(30)
print(A)
# {10, 50, 20, 30}

A.add(40)
print(A)
# {40, 10, 50, 20, 30}

A.add(50)
print(A)
# {40, 10, 50, 20, 30}
Enter fullscreen mode Exit fullscreen mode

update() can add zero or more iterables to the set as shown below:

*Memo:

  • The 1st arguments are *others(Optional-Default:()-Type:Iterable):
    • Don't use any keywords like *others=, others=, etc.
  • |= with or without | also does update() but |= and | don't support the combination of the set and list or tuple.
A = {10, 30, 50}

print(A)
# {10, 50, 30}

A.update({20, 30, 40})
A.update([20, 30, 40])
A.update((20, 30, 40))
A.update({20, 30, 40}, [20, 30, 40], (20, 30, 40))

print(A)
# {50, 20, 40, 10, 30}

A.update()

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

print(A)
# {10, 50, 30}

A |= {20, 30, 40}
A |= {20, 30, 40} | {20, 30, 40} | {20, 30, 40}

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

remove() can remove the element matched to elem from the set, getting error if the element doesn't exist in the set as shown below:

*Memo:

  • The 1st argument is elem(Required-Type:Any):
    • Don't use elem=.
A = {10, 30, 50, 70}

print(A)
# {10, 50, 70, 30}

A.remove(50)
print(A)
# {10, 70, 30}

A.remove(70)
print(A)
# {10, 30}

A.remove(20)
# KeyError: 20
Enter fullscreen mode Exit fullscreen mode

discard() can remove the element matched to elem from the set, not getting error even if the element doesn't exist in the set as shown below:

*Memo:

  • The 1st argument is elem(Required-Type:Any):
    • Don't use elem=.
A = {10, 30, 50, 70}

print(A)
# {10, 50, 70, 30}

A.discard(50)
print(A)
# {10, 70, 30}

A.discard(70)
print(A)
# {10, 30}

A.discard(20)
print(A)
# {10, 30}
Enter fullscreen mode Exit fullscreen mode

pop() can remove and throw an arbitrary element from the set as shown below:

*Memo:

  • It has no arguments.
  • Error occurs if the set is empty.
A = {10, 30, 50}

print(A) # {10, 50, 30}

print(A.pop()) # 10
print(A)       # {50, 30}

print(A.pop()) # 50
print(A)       # {30}

print(A.pop()) # 30
print(A)       # set()

print(A.pop())
# KeyError: 'pop from an empty set'
Enter fullscreen mode Exit fullscreen mode

clear() can remove all elements from the set as shown below:

*Memo:

  • It has no arguments:
A = {10, 30, 50}

print(A)
# {10, 50, 30}

A.clear()
print(A)
# set()
Enter fullscreen mode Exit fullscreen mode

union() can return all the elements of the set and *others as shown below:

*Memo:

  • The 1st arguments are *others(Optional-Default:()-Type:Iterable):
    • Don't use any keywords like *others=, others=, etc.
  • | also does union() but | doesn't support the combination of a set and list or tuple.
A = {10, 50}
B = {10, 30, 50}
C = {20, 40}

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

print(A | B)     # {50, 10, 30}
print(A | C)     # {40, 10, 50, 20
print(A | B | C) # {50, 20, 40, 10, 30}
print(A)         # {10, 50}
Enter fullscreen mode Exit fullscreen mode
A = {10, 50}     # set
B = [10, 30, 50] # list
C = (20, 40)     # tuple

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

intersection() can return the zero or more common elements of the set and *others as shown below:

*Memo:

  • The 1st arguments are *others(Optional-Default:()-Type:Iterable):
    • Don't use any keywords like *others=, others=, etc.
  • & also does intersection() but & doesn't support the combination of a set and list or tuple.
  • intersection() creates a copy while intersection_update() doesn't.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}

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

print(A & B)     # {10, 30}
print(A & C)     # {40, 30}
print(A & B & C) # {30}
print(A)         # {40, 10, 20, 30}
Enter fullscreen mode Exit fullscreen mode
A = {10, 20, 30, 40} # set
B = [10, 30, 50]     # list
C = (30, 40)         # tuple

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

intersection_update() can return the zero or more common elements of the set and *others as shown below:

*Memo:

  • The 1st arguments are *others(Optional-Default:()-Type:Iterable):
    • Don't use any keywords like *others=, others=, etc.
  • &= with or without & does intersection_update() but &= and & doesn't support the combination of a set and list or tuple.
  • intersection_update() doesn't create a copy while intersection() does.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}

A.intersection_update(B)
print(A)
# {10, 30}

A.intersection_update(C)
print(A)
# {30}

A.intersection_update(B, C)
print(A)
# {30}

A.intersection_update()
print(A)
# {30}
Enter fullscreen mode Exit fullscreen mode
A = {10, 20, 30, 40} # set
B = [10, 30, 50]     # list
C = (30, 40)         # tuple

A.intersection_update(B)
print(A)
# {10, 30}

A.intersection_update(C)
print(A)
# {30}

A.intersection_update(B, C)
print(A)
# {30}

A.intersection_update()
print(A)
# {30}
Enter fullscreen mode Exit fullscreen mode
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}

A &= B
print(A)
# {10, 30}

A &= C
print(A)
# {30}

A &= B & C
print(A)
# {30}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)