*Memos:
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}
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 ofupdate()
.
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}
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
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}
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'
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()
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 ofunion()
.
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}
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 ofintersection()
. -
intersection()
creates a copy whileintersection_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}
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 ofintersection_update()
. -
intersection_update()
doesn't create a copy whileintersection()
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}
Top comments (0)