DEV Community

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

Posted on • Edited on

Set in Python (2)

Buy Me a Coffee

*Memos:

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

add() can add an element to a set as shown below:

*Memos:

  • The 1st argument is elem(Required) for an element.
  • Don't use elem=.
A = {10, 30, 50}

A.add(20)
A.add(30)
A.add(40)
A.add(50)

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

update() can add a set 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 update().
A = {10, 30, 50}

A.update({20, 40})
# A |= {20, 40}
A.update([20, 40])
A.update((20, 40))

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

A.update()

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

remove() can remove a selected element from a set, giving error if the selected element doesn't exist in the set as shown below:

*Memos:

  • The 1st argument is elem(Required) for an element.
  • Don't use elem=.
A = {10, 30, 50}

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

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

discard() can remove a selected element from a set, not giving error even if the selected element doesn't exist in the set as shown below:

*Memos:

  • The 1st argument is elem(Required) for an element.
  • Don't use elem=.
A = {10, 30, 50}

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

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

pop() can remove and throw the last element from a set as shown below. *There are no arguments:

A = {10, 30, 50}

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 a list. *There are no arguments:

A = {10, 30, 50}

A.clear()
print(A) # set()

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:

*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 union().
A = {10, 30, 50}
B = {10, 20}
C = {30, 40}

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

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

intersection() can return the zero or more common elements of the set and *others 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 intersection().
  • intersection() creates a copy while intersection_update() doesn't.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {10, 20}

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

print(A & B) # {10, 30}
print(A & C) # {10, 20}
print(A & B & C) # {10}
print(A) # {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:

*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 intersection_update().
  • intersection_update() doesn't create a copy while intersection() does.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {10, 20}

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

A.intersection_update(C)
# A &= C
print(A) # {10}

A.intersection_update(B, C)
# A &= B & C
print(A) # {10}

A.intersection_update()
print(A) # {10}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)