Python Set – isdisjoint() Method

It is a method in python that checks if the given sets has no element in common between them. Hence it returns True if the set is disjoint else False. Two disjoint sets has no elements in common between them. It doesn’t modifies the given set and it takes single parameter in the argument.

Syntax:
set1.isdisjoint(set2)

Also Read: Python Set – copy() Method

Example 1:

# Python code to understand isdisjoint() method in set
# www.codewindow.in

s1 = {2, 5, 8, 7, 9}
s2 = {10, 18, 14}
s = {10, 5, 2}

#disjoint of s and s1
print(s.isdisjoint(s1))

#disjoint of s1 and s2
print(s1.isdisjoint(s2))

Output:

False
True

Explanation:
Here, it returned False as set s and s1 has elements common (2, 5) in them. The next one it’s True as set s1 and s2 has no elements common in them.

Example 2:

# Python code to understand isdisjoint() method in set
# www.codewindow.in

s1 = {"code", "window"}
s = {"code", "website"}
s2 = {"tech", "website"}

#disjoint of s and s1?
print(s1.isdisjoint(s))
print(s1.isdisjoint(s2))

Output:

False
True

Explanation:
Here, it returned False as set s and s1 has elements common (“code”) in them. The next one it’s True as set s1 and s2 has no elements common in them.


Follow Us

You Missed

Also Checkout