Python Set isdisjoint method with Example – Python Tutorial

6 years ago Lalit Bhagtani 0

In this tutorial, we will learn about python set isdisjoint method.
Python Set isdisjoint

Disjoint Sets:

Two sets are said to be disjoint sets, if they have no common elements or their intersection is a empty set. For example :

A = {1, 2, 3, 4, 5, 6}
B = {7, 8, 9, 10}

Python Set isdisjoint

isdisjoint method :

This method takes iterable (list, tuple, dictionary, set and string) as an argument, if iterable other than set is passed, it first converts the iterable object to set object and then checks whether the sets has any common elements or not. If one or more common elements are present, then it will return False otherwise it will return True.

Syntax : < Set Object >.isdisjoint( <iterable object> ) 

Example:

# Python Set isdisjoint # create first set object and assign it to variable s1 s1 = {1,2,3,4,5} # create second set object and assign it to variable s2 s2 = {5,6,7,8,9} # create third set object and assign it to variable s3 s3 = {6,7,8,9,10} # call isdisjoint() to check if s1 & s2 are disoint or not print('s1 and s2 are disjoint?',s1.isdisjoint(s2)) # call isdisjoint() to check if s1 & s2 are disoint or not print('s1 and s3 are disjoint?',s1.isdisjoint(s3)) # create a list object and assign it to variable l l = [4,5,6,7] # call isdisjoint() to check if s1 & l are disoint or not print('s1 and l are disjoint?',s1.isdisjoint(l)) # create string object and assign it to variable str str = '678910' # call isdisjoint() to check if s1 & str are disoint or not print('s1 and str are disjoint?',s1.isdisjoint(str))

References :-

  1. isdisjoint method Docs

That’s all for Python Set isdisjoint method. If you liked it, please share your thoughts in comments section and share it with others too.

Previous