*Memo:
difference() can return the zero or more elements which the set has but *others
don't have as shown below:
*Memos:
- 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 doesdifference()
but-
doesn't support the combination of a set and list or tuple. -
difference()
creates a copy whiledifference_update()
doesn't.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}
print(A.difference(B)) # {40, 20}
print(A.difference(C)) # {10, 20}
print(A.difference(B, C)) # {20}
print(A.difference()) # {40, 10, 20, 30}
print(A - B) # {40, 20}
print(A - C) # {10, 20}
print(A - B - C) # {20}
print(A) # {40, 10, 20, 30}
A = {10, 20, 30, 40} # set
B = [10, 30, 50] # list
C = (30, 40) # tuple
print(A.difference(B)) # {40, 20}
print(A.difference(C)) # {10, 20}
print(A.difference(B, C)) # {20}
print(A.difference()) # {40, 10, 20, 30}
difference_update() can return the zero or more elements which the set has but *others
don't have 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 doesdifference_update()
but-=
and|
don't support the combination of a set and list or tuple. -
difference_update()
doesn't create a copy whiledifference()
does.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}
A.difference_update(B)
print(A)
# {20, 40}
A.difference_update(C)
print(A)
# {20}
A.difference_update(B, C)
print(A)
# {20}
A.difference_update()
print(A)
# {20}
A = {10, 20, 30, 40} # set
B = [10, 30, 50] # list
C = (30, 40) # tuple
A.difference_update(B)
print(A)
# {20, 40}
A.difference_update(C)
print(A)
# {20}
A.difference_update(B, C)
print(A)
# {20}
A.difference_update()
print(A)
# {20}
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}
A -= B
print(A)
# {20, 40}
A -= C
print(A)
# {40}
A -= B | C
print(A)
# {40}
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:
*Memo:
- The 1st argument is
other
(Required-Type:Iterable):- Don't use
other=
.
- Don't use
-
^
also doessymmetric_difference()
but^
doesn't support the combination of a set and list or tuple. -
symmetric_difference()
creates a copy whilesymmetric_difference_update()
doesn't.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}
print(A.symmetric_difference(B)) # {40, 50, 20}
print(A.symmetric_difference(C)) # {10, 20}
print(A ^ B) # {40, 50, 20}
print(A ^ C) # {10, 20}
A = {10, 20, 30, 40} # set
B = [10, 30, 50] # list
C = (30, 40) # tuple
print(A.symmetric_difference(B)) # {40, 50, 20}
print(A.symmetric_difference(C)) # {10, 20}
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:
*Memo:
- The 1st argument is
other
(Required-Type:Iterable):- Don't use
other=
.
- Don't use
-
^=
also doessymmetric_difference_update()
but^=
doesn't support the combination of a set and list or tuple. -
symmetric_difference_update()
doesn't create a copy whilesymmetric_difference()
does.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}
A.symmetric_difference_update(B)
print(A)
# {40, 50, 20}
A.symmetric_difference_update(C)
print(A)
# {50, 20, 30}
A = {10, 20, 30, 40} # set
B = [10, 30, 50] # list
C = (30, 40) # tuple
A.symmetric_difference_update(B)
print(A)
# {40, 50, 20}
A.symmetric_difference_update(C)
print(A)
# {50, 20, 30}
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {30, 40}
A ^= B
print(A)
# {40, 50, 20}
A ^= C
print(A)
# {50, 20, 30}
isdisjoint() can check if the set and other
don't have any common elements as shown below:
*Memo:
- The 1st argument is
other
(Required-Type:Iterable):- Don't use
other=
.
- Don't use
A = {10, 20, 30}
B = {40, 50}
C = {30, 40}
print(A.isdisjoint(B)) # True
print(A.isdisjoint(C)) # False
A = {10, 20, 30} # set
B = [40, 50] # list
C = (30, 40) # tuple
print(A.isdisjoint(B)) # True
print(A.isdisjoint(C)) # False
issubset() can check if every element in the set is in other
as shown below:
*Memo:
- The 1st argument is
other
(Required-Type:Iterable):- Don't use
other=
.
- Don't use
-
<=
also doesissubset()
but<=
doesn't support the combination of a set and list, tuple or iterator.
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
A = {10, 20, 30} # set
B = [10, 20, 30, 40] # list
C = (10, 20, 30) # tuple
D = iter([10, 20]) # iterator
print(A.issubset(B)) # True
print(A.issubset(C)) # True
print(A.issubset(D)) # False
issuperset() can check if every element in other
is in the set as shown below:
*Memo:
- The 1st argument is
other
(Optional-Type:Iterable):- Don't use
other=
.
- Don't use
-
>=
also doesissuperset()
but>=
doesn't support the combination of a set and list, tuple or iterator.
A = {10, 20, 30}
B = {10, 20, 30, 40}
C = {10, 20, 30}
D = {10, 20}
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
A = {10, 20, 30} # set
B = [10, 20, 30, 40] # list
C = (10, 20, 30) # tuple
D = iter([10, 20]) # iterator
print(A.issuperset(B)) # False
print(A.issuperset(C)) # True
print(A.issuperset(D)) # True
sorted() can convert a set to a list, then sort the list, then the sorted list is converted to an unsorted set with set() as shown below:
*Memo:
- The 1st argument is
iterable
(Required-Type:Iterable):- Don't use
iterable=
.
- Don't use
- The 2nd argument is
key
(Optional-Default:None
-Type:Callable or NoneType). - The 3rd argument is
reverse
(Optional-Default:False
-Type:bool
) to reverse the list. -
sorted()
creates a copy:- Be careful,
sorted()
does shallow copy instead of deep copy as my issue.
- Be careful,
A = {3, 5, -2, 1, -4}
print(sorted(A))
print(sorted(A, key=None, reverse=False))
# [-4, -2, 1, 3, 5]
print(set(sorted(A)))
# {1, 3, 5, -4, -2}
print(set(sorted(A, reverse=True)))
# {1, 3, 5, -4, -2}
print(set(sorted(A, key=abs)))
# {1, 3, 5, -4, -2}
print(set(sorted(A, key=abs, reverse=True)))
# {1, 3, 5, -4, -2}
A = {"apple", "Banana", "Kiwi", "cherry"}
""" Case sensitive sort """
print(set(sorted(A)))
# {'apple', 'cherry', 'Banana', 'Kiwi'}
""" Case insensitive sort """
print(set(sorted(A, key=str.upper)))
print(set(sorted(A, key=str.lower)))
# {'cherry', 'Banana', 'apple', 'Kiwi'}
""" Sort by the length of a word """
print(set(sorted(A, key=len)))
# {'apple', 'cherry', 'Banana', 'Kiwi'}
Top comments (0)