• 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 / How to Check if Set is Empty in Python

How to Check if Set is Empty in Python

February 9, 2022 Leave a Comment

We can easily check if a set is empty in Python. An empty set has length 0, and is equal to False, so to check if a set is empty, we can just check one of these conditions.

empty_set = set()

#length check
if len(empty_set) == 0:
    print("Set is empty!")

#if statement check
if empty_set:
    print("Set is empty!")

#comparing to empty set
if empty_set == set():
    print("Set is empty!")

In Python, sets are a collection of elements which is unordered and mutable. When working with sets, it can be useful to be able to easily determine if the set is empty.

There are a few ways you can determine if a set is empty.

Of course, you can always test to see if the set is equal to another empty set. Second, the length of an empty set is 0. Finally, when converting an empty set to a boolean value, we get False.

In this case, we can use any one of these conditions to determine if a set is empty or not.

In the following Python code, you can see the three ways you ca check if a set is empty or not.

empty_set = set()

#length check
if len(empty_set) == 0:
    print("Set is empty!")

#if statement check
if empty_set:
    print("Set is empty!")

#comparing to empty set
if empty_set == set():
    print("Set is empty!")

Checking if Set is Empty with if Statement in Python

One fact we can use in Python to check if a set is empty is that a set that is empty is equivalent to the boolean value False.

In this case, we can test if a set is empty using a simple if statement.

empty_set = set()

#if statement check
if empty_set:
    print("Set is empty!")

Checking if Set is Empty Using Python len() Function

One of the ways we can easily check if a set is empty in Python is with the Python len() function.

The length of a set which is empty is 0.

Checking to see if a set is empty using the Python len() function is shown in the following Python code.

empty_set = set()

if len(empty_set) == 0:
    print("Set is empty!")

Checking if Set is Empty By Comparing to Another Empty Set in Python

You can also check if a set is empty by comparing it to another empty set. This is the most obvious method and works if you want to check if a tuple is empty, or check if a dictionary is empty.

Below is how to compare an empty set to another set to determine if the other set is empty or not.

empty_set = set()

if empty_set == set():
    print("Set is empty!")

Hopefully this article has been useful for you to learn how to check if a set is empty in Python.

Other Articles You'll Also Like:

  • 1.  Factorial Program in Python Using For Loop and While Loop
  • 2.  Check if Variable is Tuple in Python
  • 3.  Create List of Odd Numbers in Range with Python
  • 4.  pandas drop – Drop Rows or Columns from DataFrame
  • 5.  Get Current Year in Python
  • 6.  Python Check if Dictionary Value is Empty
  • 7.  How to Iterate through a Set Using Python
  • 8.  Python ljust Function – Left Justify String Variable
  • 9.  Creating a Random Color Turtle in Python
  • 10.  Mastering Data Transformation with Pandas Pivot Tables

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