• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
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.  Using Python to Replace Backslash in String
  • 2.  Add Seconds to Datetime Variable Using Python timedelta() Function
  • 3.  Python Turtle Fonts – How to Write Text with Different Fonts in Python
  • 4.  Get Days of timedelta Object in Python
  • 5.  Python min() function – Get Minimum of List or Dictionary with min() Function
  • 6.  Print Time Elapsed in Python
  • 7.  Using Selenium to Close Browser in Python
  • 8.  Get Days in Month from Date in pandas DataFrame
  • 9.  Union of Lists in Python
  • 10.  How to Multiply Two Numbers 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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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