• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Python issubset() Function – Check if Set is Subset of Another Set

Python issubset() Function – Check if Set is Subset of Another Set

July 15, 2022 Leave a Comment

The Python issubset() function allows you to check if a set is a subset of another set.

a = {1, 2, 3}
b = {1, 2, 3, 4, 5, 6}

print(a.issubset(b))

#Output:
True

When working with different collections of data, the ability to easily determine properties of these objects can be useful.

One such property is if a set is a subset of another set.

A set X is a subset of another set Y if all of the elements of the set X are in the set Y.

In Python, you can use the issubset() function to check if a set is a subset of another set. issubset() returns a boolean value.

Below is a simple example showing you how to check if a set is a subset of another set using the issubset() function in Python.

a = {1, 2, 3}
b = {1, 2, 3, 4, 5, 6}

print(a.issubset(b))

#Output:
True

Using < and ≤ Operators to Check if Set is Subset in Python

In addition to the issubset() function, you can also use the < and ≤ operators to check if a set is a subset of another set.

The < operator checks if the subset is a proper subset, and ≤ checks if the set is contained in the other set with chance for equality.

Below are some examples showing how to use the < and ≤ operators to check if a set is a subset of another set in Python.

a = {1, 2, 3}
b = {1, 2, 3, 4, 5, 6}

print(a < a)
print(a < b)
print(a <= a)
print(a <= b)

#Output:
False
True
True
True

Using Python issuperset() Function to Check if Set is Superset of Another Set

If you want to go the other way and check if a set is a superset of another set, or that all of the elements of the set are also in the other set, then you can use the issuperset() function.

Just like issubset(), issuperset() returns a boolean value.

Below is a simple example showing you how to check if a set is a subset of another set using the issubset() function in Python.

a = {1, 2, 3}
b = {1, 2, 3, 4, 5, 6}

print(b.issuperset(a))

#Output:
True

Hopefully this article has been useful for you to learn how to use the issubset() function in Python.

Other Articles You'll Also Like:

  • 1.  Remove Trailing Zeros from String with rstrip() in Python
  • 2.  Transpose DataFrame pandas – Using the pandas transpose Function
  • 3.  Does Python Use Semicolons? Why Python Doesn’t Use Semicolons
  • 4.  Using Python to Find Second Largest Value in List
  • 5.  math.radians() python – How to Convert Degrees to Radians in Python
  • 6.  Python atan2 – Find Arctangent of the Quotient of Two Numbers
  • 7.  Using Python to Sum the Digits of a Number
  • 8.  Using Python to Get Home Directory
  • 9.  Python Get Yesterday’s Date
  • 10.  How to Remove NaN from List in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy