*Memo:
add() can add a value to the set as shown below:
*Memo:
- The 1st argument is
elem
(Required-Type:Any):- Don't use
elem=
.
- Don't use
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}
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.
- Don't use any keywords like
-
|=
with or without|
also doesupdate()
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}
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}
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=
.
- Don't use
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
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=
.
- Don't use
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}
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'
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()
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.
- Don't use any keywords like
-
|
also doesunion()
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}
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}
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.
- Don't use any keywords like
-
&
also doesintersection()
but&
doesn't support the combination of a set and list or tuple. -
intersection()
creates a copy whileintersection_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}
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}
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.
- Don't use any keywords like
-
&=
with or without&
doesintersection_update()
but&=
and&
doesn't support the combination of a set and list or tuple. -
intersection_update()
doesn't create a copy whileintersection()
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}
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}
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}
Top comments (0)