How to Check If a Set Is a Subset of Another Set in Python

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn how to determine if one set is a subset of another set in Python. You'll start by understanding the concept of subsets and creating two sets, set1 and set2, to explore their relationship. Using the issubset() method, you'll write a Python script to check if set1 is a subset of set2, observing the output True or False based on whether all elements of set1 are present in set2.

The lab then delves deeper into the issubset() method, exploring different scenarios to solidify your understanding. You'll modify the elements of the sets and rerun the script to observe how the output changes, reinforcing the concept of subsets and the functionality of the issubset() method.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python(("Python")) -.-> python/PythonStandardLibraryGroup(["Python Standard Library"]) python/DataStructuresGroup -.-> python/sets("Sets") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") python/PythonStandardLibraryGroup -.-> python/data_collections("Data Collections") subgraph Lab Skills python/sets -.-> lab-559563{{"How to Check If a Set Is a Subset of Another Set in Python"}} python/build_in_functions -.-> lab-559563{{"How to Check If a Set Is a Subset of Another Set in Python"}} python/data_collections -.-> lab-559563{{"How to Check If a Set Is a Subset of Another Set in Python"}} end

Understand Subsets

In this step, you will learn about subsets in Python. A subset is a set whose elements are all contained in another set. Understanding subsets is crucial for various programming tasks, such as data validation, filtering, and relationship analysis.

Let's start by creating two sets:

set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}

Here, set1 contains the elements 1, 2, and 3, while set2 contains the elements 1, 2, 3, 4, and 5. We want to determine if set1 is a subset of set2.

To do this, you'll create a Python script named subset_check.py in the ~/project directory using the VS Code editor. Open VS Code and create a new file named subset_check.py in the ~/project directory.

Now, add the following code to subset_check.py:

set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}

is_subset = set1.issubset(set2)

print(is_subset)

Save the file.

Next, execute the script using the python command in the terminal:

python subset_check.py

The output will be:

True

This indicates that set1 is indeed a subset of set2 because all elements in set1 are also present in set2.

Now, let's consider another example where set1 is not a subset of set2:

Modify the subset_check.py file to the following:

set1 = {1, 2, 6}
set2 = {1, 2, 3, 4, 5}

is_subset = set1.issubset(set2)

print(is_subset)

Save the file and run it again:

python subset_check.py

The output will be:

False

This is because set1 contains the element 6, which is not present in set2. Therefore, set1 is not a subset of set2.

Use issubset() Method

In the previous step, you learned the basic concept of subsets and saw a simple example using the issubset() method. In this step, we will explore the issubset() method in more detail with different scenarios.

The issubset() method is a built-in Python method that you can use with sets. It returns True if all elements of a set are present in another set (the set is a subset), and False otherwise.

Let's consider a scenario where we have two sets representing skills:

skills1 = {"Python", "Data Analysis"}
skills2 = {"Python", "Data Analysis", "Machine Learning", "SQL"}

Here, skills1 represents the skills of a junior data scientist, and skills2 represents the skills required for a senior data scientist. We want to check if the skills of the junior data scientist are a subset of the skills required for the senior data scientist.

Open the subset_check.py file in the ~/project directory using VS Code. Modify the content of the file to the following:

skills1 = {"Python", "Data Analysis"}
skills2 = {"Python", "Data Analysis", "Machine Learning", "SQL"}

is_subset = skills1.issubset(skills2)

print(is_subset)

Save the file.

Now, execute the script using the python command in the terminal:

python subset_check.py

The output will be:

True

This indicates that skills1 is a subset of skills2, meaning the junior data scientist's skills are a subset of the senior data scientist's required skills.

Let's consider another scenario where skills1 is not a subset of skills2:

Modify the subset_check.py file to the following:

skills1 = {"Python", "Data Analysis", "Cloud Computing"}
skills2 = {"Python", "Data Analysis", "Machine Learning", "SQL"}

is_subset = skills1.issubset(skills2)

print(is_subset)

Save the file and run it again:

python subset_check.py

The output will be:

False

This is because skills1 contains "Cloud Computing", which is not present in skills2. Therefore, skills1 is not a subset of skills2.

Check with <= Operator

In the previous step, you learned how to use the issubset() method to check if one set is a subset of another. In this step, you will learn an alternative way to check for subsets using the <= operator.

The <= operator can be used to check if one set is a subset of another. It returns True if the set on the left side is a subset of the set on the right side, and False otherwise.

Let's revisit the skills example from the previous step:

skills1 = {"Python", "Data Analysis"}
skills2 = {"Python", "Data Analysis", "Machine Learning", "SQL"}

We want to check if skills1 is a subset of skills2 using the <= operator.

Open the subset_check.py file in the ~/project directory using VS Code. Modify the content of the file to the following:

skills1 = {"Python", "Data Analysis"}
skills2 = {"Python", "Data Analysis", "Machine Learning", "SQL"}

is_subset = skills1 <= skills2

print(is_subset)

Save the file.

Now, execute the script using the python command in the terminal:

python subset_check.py

The output will be:

True

This indicates that skills1 is indeed a subset of skills2.

Let's consider another scenario where skills1 is not a subset of skills2:

Modify the subset_check.py file to the following:

skills1 = {"Python", "Data Analysis", "Cloud Computing"}
skills2 = {"Python", "Data Analysis", "Machine Learning", "SQL"}

is_subset = skills1 <= skills2

print(is_subset)

Save the file and run it again:

python subset_check.py

The output will be:

False

This is because skills1 contains "Cloud Computing", which is not present in skills2. Therefore, skills1 is not a subset of skills2.

It's important to note that the <= operator also returns True if the two sets are equal. For example:

set1 = {1, 2, 3}
set2 = {1, 2, 3}

is_subset = set1 <= set2

print(is_subset)

The output will be True because set1 and set2 are equal. If you want to check if set1 is a proper subset of set2 (i.e., set1 is a subset of set2 but not equal to set2), you can use the < operator instead.

Summary

In this lab, you learned how to check if a set is a subset of another set in Python. You started by understanding the concept of subsets, where a set is considered a subset if all its elements are contained within another set.

You then utilized the issubset() method to determine if one set is a subset of another. By creating Python scripts and executing them, you observed how the issubset() method returns True when all elements of the first set are present in the second set, and False otherwise.