*Memos:
difference() can return the zero or more elements which the set has but *others
don't have 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 ofdifference()
. -
difference()
creates a copy whiledifference_update()
doesn't.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {10, 20}
print(A.difference(B)) # {40, 20}
print(A.difference(C)) # {40, 30}
print(A.difference(B, C)) # {40}
print(A - B) # {40, 20}
print(A - C) # {40, 30}
print(A - B - C) # {40}
difference_update() can return the zero or more elements which the set has but *others
don't have 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 ofdifference_update()
. -
difference_update()
doesn't create a copy whiledifference()
does.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {10, 20}
A.difference_update(B)
# A -= B
print(A) # {20, 40}
A.difference_update(C)
# A -= C
print(A) # {40}
A.difference_update(B, C)
# 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:
*Memos:
- The 1st argument is
*other
(Optional-Type:iterable
) for zero or one iterable. - Don't use any keywords like
*other=
,other=
,other=
, etc. - You can use
^
with sets instead ofsymmetric_difference()
. -
symmetric_difference()
creates a copy whilesymmetric_difference_update()
doesn't.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {10, 20}
print(A.symmetric_difference(B)) # {40, 50, 20}
print(A.symmetric_difference(C)) # {40, 30}
print(A ^ B) # {40, 50, 20}
print(A ^ C) # {40, 30}
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:
*Memos:
- The 1st argument is
*other
(Optional-Type:iterable
) for zero or more iterable. - Don't use any keywords like
*other=
,other=
,other=
, etc. - You can use
^=
with sets instead ofsymmetric_difference_update()
. -
symmetric_difference_update()
doesn't create a copy whilesymmetric_difference()
does.
A = {10, 20, 30, 40}
B = {10, 30, 50}
C = {10, 20}
A.symmetric_difference_update(B)
# A ^= B
print(A) # {40, 50, 20}
A.symmetric_difference_update(C)
# A ^= C
print(A) # {40, 10, 50}
isdisjoint() can check if the set and *other
don't have any common elements as shown below:
*Memos:
- The 1st argument is
*other
(Optional-Type:iterable
) for an iterable. - Don't use any keywords like
*other=
,other=
,other=
, etc.
A = {10, 20, 30}
B = {40, 50}
C = {30, 40}
print(A.isdisjoint(B)) # True
print(A.isdisjoint(C)) # False
You can use issubset() and issuperset() as shown below:
*Memos:
-
issubset()
can check if every element in the set is in*other
: -
issuperset()
can check if every element in*other
is in the set: *Memos:- The 1st argument is
*other
(Optional-Type:iterable
) for an iterable. - Don't use any keywords like
*other=
,other=
,other=
, etc.
- The 1st argument is
- You can use
<=
and>=
with sets instead ofissubset()
andissuperset()
respectively.
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
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
Top comments (0)