Python Set Superset with Example – Python Tutorial

6 years ago Lalit Bhagtani 1

In this tutorial, we will learn about different ways of checking superset relationship in a given pair of set.
Python Set Superset

Superset Set:

In set theory, a set A is a superset of a set B, if B is contained inside A which means that all elements of a set B are also elements of a set A. For example :

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

Here, A is superset of B :
A ⊇ B 

Python Set Superset

Python Set Superset :

In Python, there are two different ways of checking whether a given pair of sets are in a superset relationship or not, they are as follows :

  1. issuperset Method
  2. >= Operator

issuperset 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 all elements of a set ( passed as an argument ) are also present in a set ( on which issuperset method is invoked ). If yes then it will return True otherwise it will return False.

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

Example:

# Python Set Superset # create first set object and assign it to variable A A = {1,2,3,4,5,6,7,8,9,10,11,12} # create second set object and assign it to variable B B = {4,3,7,8,11} # call issuperset() to check if A is Superset of B? print('A is Superset of B?',A.issuperset(B)) # call issuperset() to check if B is Superset of A? print('B is Superset of A?',B.issuperset(A)) # create a list object and assign it to variable L L = [4,3,7,8,11,12] # call issuperset() to check if B is Superset of L? print('A is Superset of L?',A.issuperset(L))

>= Operator :

This operator is used check whether a given pair of sets are in a superset relationship or not just like issuperset() method. The difference between >= operator and issuperset() method is that, the former can work only with set objects while latter can work with any iterable.

Syntax : < Set Object 1 > >= < Set Object 2 > : To check superset relationship

              < Set Object 1 > > < Set Object 2 > : To check proper superset relationship

Example:

# Python Set Superset # create first set object and assign it to variable A A = {1,2,3,4,5,6,7,8,9,10,11,12} # create second set object and assign it to variable B B = {4,3,7,8,11} # create second set object and assign it to variable C C = {1,2,3,4,5,6,7,8,9,10,11,12} # use >= operator to check if A is Superset of B? print('A is Superset of B?',A >= B) # use >= operator to check if B is Superset of A? print('B is Superset of A?',B >= A) # use > operator to check if B is Strict Superset of A? print('A is Proper Superset of B?',A > B) # use > operator to check if C is Strict Superset of A? print('C is Proper Superset of A?',C > A)

References :-

  1. issuperset method Docs

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

Previous