Home »
Python
Python Set isdisjoint() Method with Example
Python Set isdisjoint() Method: Here, we are going to learn check whether there are common elements in two sets or not in Python?
Submitted by IncludeHelp, on November 29, 2019
Set isdisjoint() Method
isdisjoint() method is used to check whether elements are common in two sets or not, method is called with one set (commonly known as this set) and another set is supplied as an argument, method returns "True" if none of the elements are common in both sets, else it returns "False".
Syntax:
set1.isdisjoint(set2)
Parameter(s):
- set1 – It represents the set1 (this set).
- set2 – It represents the set2 (another set to be compared).
Return value:
The return type of this method is <class 'bool'>, it returns a Boolean value (True or False).
Example 1:
# Python Set isdisjoint() Method with Example
# declaring the sets
cars_1 = {"Porsche", "Audi", "Lexus"}
cars_2 = {"Porsche", "Mazda", "Lincoln"}
cars_3 = {"BMW", "Mazda", "Lincoln"}
# isdisjoint() method call
result = cars_1.isdisjoint(cars_2)
print("cars_1.isdisjoint(cars_2): ", result)
result = cars_2.isdisjoint(cars_3)
print("cars_2.isdisjoint(cars_3): ", result)
result = cars_1.isdisjoint(cars_3)
print("cars_1.isdisjoint(cars_3): ", result)
Output
cars_1.isdisjoint(cars_2): False
cars_2.isdisjoint(cars_3): False
cars_1.isdisjoint(cars_3): True
Example 2:
# Python Set isdisjoint() Method with Example
# declaring the sets
x = {"ABC", "PQR", "XYZ"}
y = {"ABC", "PQR", "XYZ"}
z = {"DEF", "MNO", "UVW"}
# isdisjoint() method calls
result = x.isdisjoint(y)
print("x.isdisjoint(y): ", result)
result = y.isdisjoint(z)
print("y.isdisjoint(z): ", result)
result = z.isdisjoint(x)
print("z.isdisjoint(x): ", result)
result = x.isdisjoint(z)
print("x.isdisjoint(z): ", result)
result = y.isdisjoint(x)
print("y.isdisjoint(x): ", result)
Output
x.isdisjoint(y): False
y.isdisjoint(z): True
z.isdisjoint(x): True
x.isdisjoint(z): True
y.isdisjoint(x): False
TOP Interview Coding Problems/Challenges