Python Set isdisjoint ,set issubset() and set issuperset()

Here we will see three methods of set,the Python set isdisjoint(),set issubset() and set issuperset().

i)set disjoint
ii)set issubset
iii)set isuperset


i)Set isdisjoint

The set disjoint method returns true if there are no common elements in the sets,if there are any common element the method return false.This function accept one argument.

The set whose elements is to be checked against is pass as an argument.

s1={23, 89 , 'm' , 'new' }
>>> s2={'Text' , 'nEw' , 9028 }
>>> s3={ 23 , 'TeXt' , 9000 }
>>> s1.isdisjoint( s2 )
True
>>> s1.isdisjoint( s3 )
False
>>> s2.isdisjoint( s3 )
True

The s1 and s2 has no common element so we get True,s1 and s3 has one common element so we get False and s2 and s3 has no common element so we get True.



i)Set issubset

The issubset() function determine if the the set through which isusbset() is called is a subset of the set pass as the argument.To understand what this means consider example.
 
s1.issubset( s2 )
 
The above call will test if s1 is a subset of s2.

Instead of using this issubset() you can also the ‘<‘ or ‘<=‘ operator to determine whether the set is a subset of another set.

Consider set1 and set2 as two sets.Here is how you should use the operators.
 
set1 <= set2
 
Test whether every element in the set1 is in set2.
 
set < other
 
Test whether the set1 is a proper subset of set2, that is, set1 <= set2 and set1 != set2.

Consider the code below.

>>> s1={12 ,67 , 89 ,'New'}
>>> s2={12 , 'new' , 'text'}
>>> s3={ 67 , 89 }
>>> s4={ 12 , 'new' , 'text'}
>>> s1.issubset( s2 )
False
>>> s3.issubset( s1 )
True
>>> s2.issubset( s4 )
True


Here is another example using the operators.

>>> s1={12 ,67 , 89 ,'New'}
>>> s2={12 , 'new' , 'text'}
>>> s3={ 67 , 89 }
>>> s4={ 12 , 'new' , 'text'}
>>> s2< s1 
False
>>> s3<s1
True
>>> s2<= s4
True

I am sure the example above needs no further explanation.



iii)Set issuperset

The Python issuperset() function test if one set is a superset of another set.

Consider s1 and s2 as two sets.If the call is made as shown below:
 
s1.issuperset( s2 )
 
It test if s1 is a superset of s2.

To test superset we can also use the operator ‘>=‘ and ‘>‘.
 
set1 >= set2
 
Test whether every element in set2 is in the set1.
 
set1 > set2
 
Test whether the set1 is a proper superset of set2, that is, set1 >= set2 and set1 != set2.

>>> s1={ 34 ,901 , 89 , 'king'}
>>> s2={ 34 , 'text' , 'Heap' , 89 , 901 ,1000 , 'king'}
>>> s3={ 89 , 'king' }
>>> s4={ 'king' , 89 , 901, 34 }
>>> s2.issuperset( s1 )
True
>>> s1.issuperset( s4 )
True
>>> s1.issuperset( s3 )
True
>>> s3.issuperset( s3 )
True
>>> s3.issuperset( s2 )
False

Another example is shown below using the operators ‘>=’ and ‘>’.

>>> s1={ 34 ,901 , 89 , 'king'}
>>> s2={ 34 , 'text' , 'Heap' , 89 , 901 ,1000 , 'king'}
>>> s3={ 89 , 'king' }
>>> s4={ 'king' , 89 , 901, 34 }
>>> s1>=s4
True
>>> s3>=s3
True
>>> s1>s4
False