Python Set issubset() Method with Examples

Prerequisite:

Python set() Function with Examples

Set issubset() Method in Python:

If all of the items in the set exist in the specified set, the issubset() method returns true; otherwise, it returns False.

For Example,

Let p = {1, 2, 3}

q= {1, 2, 3, 4, 5, 6}

Here all the elements of set ‘p ‘ are in the set ‘q’ .Hence set p is a subset of set q.

Syntax:

set.issubset(set)

Parameters

set: This is  Required. It is the set to look for items that are similar.

Return Value:

issubset() produces a result.

  • If A is a subset of B, then this statement is true.
  • If A is not a subset of B, the statement is false.

Examples:

Example1:

Input:

Given first set = {20, 30, 100}
Given second set = {100, 20, 80, 70, 30}

Output:

Check if the given first set is subset of the second set: True

Example2:

Input:

Given first set = {3, 5, 6, 1}
Given second set = {3, 7, 8 ,9}

Output:

Check if the given first set is subset of the second set: False

Set issubset() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first set as static input and store it in a variable.
  • Give the second set as static input and store it in another variable.
  • Apply the issubset() method to the given first and second sets to check if the given first set is a subset of the second set or not.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_set = {20, 30, 100}
# Give the second set as static input and store it in another variable.
scnd_set = {100, 20, 80, 70, 30}
# Apply the issubset() method to the given first and second sets to check if the
# given first set is subset of the second set or not.
# Store it in another variable.
rslt = fst_set.issubset(scnd_set)
# Print the above result.
print("Check if the given first set is subset of the second set:", rslt)

Output:

Check if the given first set is subset of the second set: True
Similarly, check for the other example
fst_set = {'a', 'l', 'm'}
scnd_set = {'m', 'a', 'l'}
rslt = fst_set.issubset(scnd_set)
print("Check if the given first set is subset of the second set:", rslt)

Output:

Check if the given first set is subset of the second set: True

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first set as user input using set(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Apply the issubset() method to the given first and second sets to check if the given first set is a subset of the second set or not.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))

# Apply the issubset() method to the given first and second sets to check if the
# given first set is subset of the second set or not.
# Store it in another variable.
rslt = fst_set.issubset(scnd_set)
# Print the above result.
print("Check if the given first set is subset of the second set:", rslt)

Output:

Enter some random Set Elements separated by spaces = 3 5 6 1
Enter some random Set Elements separated by spaces = 3 7 8 9
Check if the given first set is subset of the second set: False