# Python Set isdisjoint()

Python’s `set.isdisjoint(set)` returns `True` if no element from this set is a member of the specified set. Sets are disjoint if and only if their intersection is the empty set.

Here’s a minimal example that checks whether sets `s` and `t` are disjoint:

```>>> s = {1, 2, 3, 4}
>>> t = {'Alice', 'Bob'}
>>> s.isdisjoint(t)
True```

## Syntax

Let’s dive into the formal syntax of the `set.isdisjoint()` method.

`set.isdisjoint(set)`

## Return Value of set.isdisjoint()

The return value of `set.isdisjoint()` is a Boolean whether the set is disjoint with the set defined as an argument.

There are some subtleties you need to understand regarding the set disjoint method. Let’s dive into them by example!

We start with a simple and trivial example of two disjoint sets:

```>>> {1, 2, 3}.isdisjoint({'Alice', 'Bob'})
True```

? Can you also pass a list as an argument to the `set.isdisjoint()` method? The answer is yes—the method takes any iterable.

```>>> {1, 2, 3}.isdisjoint(['Alice', 'Bob'])
True```

Let’s have an example of a non-disjoint set combination that intersect in one element—can you figure out which?

```>>> {1, 2, 3}.isdisjoint({'Alice', 'Bob', 1})
False```

Are two empty sets disjoint? Yes—they have no common elements. The intersection between two empty sets is empty, so they are disjoint!

```>>> set().isdisjoint(set())
True```

The same holds if one of the sets is non-empty:

```>>> set().isdisjoint({1, 2, 3})
True
>>> {1, 2, 3}.isdisjoint(set())
True```

Can we pass multiple set arguments into the `set.isdisjoint()` method? No! It only takes one argument.

```>>> {1, 2, 3}.isdisjoint({4, 5}, {'Alice', 'Bob'})
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
{1, 2, 3}.isdisjoint({4, 5}, {'Alice', 'Bob'})
TypeError: isdisjoint() takes exactly one argument (2 given)```

## What is the Time Complexity of set.isdisjoint() in Python?

The worst-case runtime complexity of the `set.disjoint()` method is the same as the `set.intersection()` method because it first computes the intersection of both sets and then checks if the intersection is empty to determine whether the sets are disjoint. For a set with n elements and a set argument with m elements, the runtime complexity is O(min(n, m)) because you need to check for the smaller set whether each of its elements is a member of the larger set. However, in practice, the runtime can be much faster because the first common element found already determines the answer and the whole computation can be aborted.

You can see this in the following simple experiment where we run the set method multiple times for increasing set sizes:

I ran this experiment on my Acer Aspire 5 notebook (I know) with Intel Core i7 (8th Gen) processor and 16GB of memory. Here’s the code of the experiment:

```import matplotlib.pyplot as plt
import time

sizes = [i * 10**5 for i in range(50)]
runtimes = []

for size in sizes:
s = set(range(size))
t = set(range(0, size, 2))

# Start track time ...
t1 = time.time()
s.isdisjoint(t)
t2 = time.time()
# ... end track time

runtimes.append(t2-t1)

plt.plot(sizes, runtimes)
plt.ylabel('Runtime (s)')
plt.xlabel('Set Size')

plt.show()
```

The runtime complexity feels constant. Only if we slightly modify the experiment to ensure that the sets are not disjoint will we see an increase of the runtime complexity with increasing set sizes:

```import matplotlib.pyplot as plt
import time

sizes = [i * 10**5 for i in range(50)]
runtimes = []

for size in sizes:
s = set(range(size))
t = set(range(size, 2* size, 1))

# Start track time ...
t1 = time.time()
s.isdisjoint(t)
t2 = time.time()
# ... end track time

runtimes.append(t2-t1)

plt.plot(sizes, runtimes)
plt.ylabel('Runtime (s)')
plt.xlabel('Set Size')

plt.show()
```

## Other Python Set Methods

All set methods are called on a given set. For example, if you created a set `s = {1, 2, 3}`, you’d call `s.clear()` to remove all elements of the set. We use the term “this set” to refer to the set on which the method is executed.