Ref Set Isdisjoint
## Python Set isdisjoint() Method
In Python, a set is an unordered collection of unique elements. The `isdisjoint()` method is a built-in set operation used to determine whether two sets have any elements in common.
This tutorial covers the syntax, parameters, return values, and practical use cases of the `isdisjoint()` method.
---
## Description
The `isdisjoint()` method checks whether two sets are **disjoint**. Two sets are considered disjoint if their intersection is an empty set ($\emptyset$).
* Returns `True` if the two sets have **no elements in common**.
* Returns `False` if there is **at least one common element** between them.
---
## Syntax
```python
set.isdisjoint(iterable)
```
### Parameters
* **`iterable`** (Required): The collection to compare with the original set. While typically another `set`, this parameter can be any iterable object (such as a `list`, `tuple`, `dictionary`, or `string`). Python will internally convert the iterable to evaluate the intersection.
### Return Value
* Returns a **boolean** value:
* `True`: The sets share zero common elements.
* `False`: The sets share one or more common elements.
---
## Code Examples
### Example 1: Disjoint Sets (No Common Elements)
In this example, set `x` and set `y` contain completely different elements.
```python
# Define two sets with no overlapping elements
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "facebook"}
# Check if they are disjoint
result = x.isdisjoint(y)
print(result)
```
**Output:**
```text
True
```
---
### Example 2: Non-Disjoint Sets (With Common Elements)
In this example, both set `x` and set `y` contain the element `"apple"`.
```python
# Define two sets with a common element ("apple")
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
# Check if they are disjoint
result = x.isdisjoint(y)
print(result)
```
**Output:**
```text
False
```
---
### Example 3: Using `isdisjoint()` with Other Iterables
The `isdisjoint()` method does not require the argument to be a set. You can pass other iterables like lists or dictionaries.
```python
# Define a set
fruits = {"apple", "banana", "cherry"}
# Compare with a list
fruits_list = ["google", "facebook"]
print(fruits.isdisjoint(fruits_list)) # Output: True
# Compare with a dictionary (checks against dictionary keys)
fruits_dict = {"apple": 1, "orange": 2}
print(fruits.isdisjoint(fruits_dict)) # Output: False (since "apple" is a key)
```
**Output:**
```text
True
False
```
---
## Considerations & Performance
1. **Short-Circuit Evaluation**: The `isdisjoint()` method is highly efficient. It performs short-circuit evaluation, meaning it stops iterating and returns `False` the moment it finds the first matching element.
2. **Time Complexity**: In the average case, the time complexity is $O(\min(len(s), len(t)))$ where $s$ is the set and $t$ is the iterable argument. This makes it much faster than manually checking intersections using loops.
3. **Operator Equivalent**: Unlike other set methods (like `intersection()` which has the `&` operator), there is no direct operator equivalent for `isdisjoint()`. However, it is logically equivalent to checking if the intersection is empty: `len(x & y) == 0` or `x.intersection(y) == set()`. Using `isdisjoint()` is preferred as it is more readable and performant.
YouTip