A set is an unordered collection of unique elements. Python provides several built-in methods for manipulating sets, including the isdisjoint()
method. The isdisjoint()
method returns a Boolean value indicating whether the set has no common elements with the given set(s).
Syntax
set.isdisjoint(set)
The isdisjoint()
method takes one argument as a set and returns True
if the set has no common elements with the given set. Otherwise, it returns False
.
Return Value
- If the set has no common elements with the given set, it returns
True
. - If the set has common elements with the given set, it returns
False
.
Examples
Example 1: Using isdisjoint() method with two sets
Let’s create two sets and check if they have any common elements using the isdisjoint()
method:
# Create two sets set1 = {1, 2, 3, 4, 5} set2 = {6, 7, 8, 9, 10} # Check if the sets have any common elements result = set1.isdisjoint(set2) # Print the result print(result)
Output:
True
In the above example, we created two sets (set1
and set2
) and checked if they have any common elements using the isdisjoint()
method. Since the sets have no common elements, the isdisjoint()
method returns True
.
Example 2: Using isdisjoint() method with three sets
Let’s create three sets and check if they have any common elements using the isdisjoint()
method:
# Create three sets set1 = {1, 2, 3, 4, 5} set2 = {6, 7, 8, 9, 10} set3 = {3, 4, 5} # Check if the sets have any common elements result1 = set1.isdisjoint(set2) result2 = set1.isdisjoint(set3) # Print the results print(result1) print(result2)
Output:
True False
In the above example, we created three sets (set1
, set2
, and set3
) and checked if they have any common elements using the isdisjoint()
method. The first check returns True
since set1
and set2
have no common elements, while the second check returns False
since set1
and set3
have common elements (3, 4, 5).
Example 3: Using isdisjoint() method with an empty set
Let’s create an empty set and check if it is disjoint from a non-empty set using the isdisjoint()
method:
# Create an empty set and a non-empty set set1 = set() set2 = {1, 2, 3, 4, 5} # Check if the sets have any common elements result = set1.isdisjoint(set2) # Print the result print(result)
Output:
True
In the above example, we created an empty set (set1
) and a non-empty set (set2
) and checked if they have any common elements using the isdisjoint()
method. Since the empty set has no elements, it is disjoint from any other set, and the isdisjoint()
method returns True
.
Use Cases
The isdisjoint()
method can be useful in various scenarios where you need to check if two sets have any common elements. For example, you can use it to:
- Check if two lists have any common elements by converting them to sets.
- Check if two words are anagrams (have the same letters but in a different order) by converting them to sets and checking if they are disjoint.
- Check if two strings have any common characters by converting them to sets and checking if they are disjoint.
Overall, the isdisjoint()
method provides a simple and efficient way to check if two sets have any common elements.