Click for All Topics
Syntax –
set_name.isdisjoint(iterable)
Parameter –
Example –
set1={1,3,5,7,9}
set2={2,4,6,8}
print("Set 1:",set1)
print("Set 2:",set2)
s=set1.isdisjoint(set2)
print("Answer:",s)
# Output -
# Set 1: {1, 3, 5, 7, 9}
# Set 2: {8, 2, 4, 6}
# Answer: True
.isdisjoint()
method with set1 passed set2 as an argument and stored the result in the s variable.True
because there is no common element between both the sets.
The .isdisjoint()
method with another iterable –
Example –
my_set={"Hello","this","is","edSlash"}
string="Anurag Dubey"
print("My set:",my_set)
print("My string:",string)
print(my_set.isdisjoint(string))
# Output -
# My set: {'edSlash', 'is', 'Hello', 'this'}
# My string: Anurag Dubey
# True
Checking whether two sets are disjoint without using the inbuilt method –
Example –
s1={1,2,3,4,5,6}
s2={2,4,6,8,10}
answer=True
for element in s1:
if element in s2:
answer=False
break
print("Set 1:",s1)
print("Set 2:",s2)
print(answer)
# Output -
# Set 1: {1, 2, 3, 4, 5, 6}
# Set 2: {2, 4, 6, 8, 10}
# False
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India