English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriel de base Python

Contrôle de flux Python

Fonctions en Python

Types de données en Python

Opérations sur les fichiers Python

Objets et classes Python

Dates et heures Python

Connaissances avancées Python

Manuel de référence Python

Utilisation et exemples de la méthode isdisjoint() de la collection Python

Méthodes de collection en Python

If two sets are disjoint sets, the isdisjoint() method returns True. If not, it returns False.

If two sets have no common elements, they are called disjoint sets. For example:

A = {1, 5, 9, 0}
B = {2, 4, -5}

In this case, set A and B are disjoint sets.

The syntax of isdisjoint() is:

set_a.isdisjoint(set_b)

isdisjoint() parameters

The isdisjoint() method takes a single parameter (a group).

You can also pass an iterable (list, tuple, dictionary, and string) to isdisjoint(). The isdisjoint() method will automatically convert the iterable object to a set and check if these sets are disjoint.

The return value of isdisjoint()

The isdisjoint() method returns

  • True if the two sets are disjoint sets (in the above syntax if set_a and set_b are disjoint sets)

  • False if the two sets are not disjoint sets

Example1: How does isdisjoint() work?

A = {1, 2, 3, 4}
B = {5, 6, 7}
C = {4, 5, 6}
print('A and B are disjoint?', A.isdisjoint(B))
print('A and C are disjoint?', A.isdisjoint(C))

When running this program, the output is:

Are A and B disjoint? True
Are A and C disjoint? False

Example2: Pass isdisjoint() with other Iterables as parameters

A = {'a', 'b', 'c', 'd'}
B = ['b', 'e', 'f']
C = '5de4'
D = {1 : 'a', 2 : 'b'}
E = {'a' : 1, 'b' : 2}
print('A and B are disjoint?', A.isdisjoint(B))
print('A and C are disjoint?', A.isdisjoint(C))
print('A and D are disjoint?', A.isdisjoint(D))
print('A and E are disjoint?', A.isdisjoint(E))

When running this program, the output is:

Are A and B disjoint? False
Are A and C disjoint? False
Are A and D disjoint? True
Are A and E disjoint? False

Méthodes de collection en Python