Check If Set is Empty in Python

I was working on a data-cleaning project where I had to manage a large collection of unique values. Since I was using Python sets, I often needed to check whether a set was empty before performing operations on it.

The challenge was that Python does not have a direct function like isEmpty() for sets. Instead, we rely on a few simple and efficient techniques.

In this tutorial, I will share six practical methods I use to check if a Python set is empty. I’ll also explain when to use each method, so you can pick the one that works best for your project.

Method 1. Use the Python len() function

The len() function in Python is used to find the length of an iterable. It returns the number of items present inside those iterables.

Here is an example of Python to check if a set is empty:

States = {'Alaska', 'Texas', 'New York'}
print('The length of the States set: ', len(States))

Countries = {}
print('The length of the Countries set: ', len(Countries))

Output:

The length of the States set:  3
The length of the Countries set:  0

You can refer to the screenshot below to see the output:

How to Check if a Python Set is Empty

For the set in Python, if the length is zero, it means that the set is empty in Python.

Method 2. Use the bool() function in Python

The bool() function in Python returns the Boolean value of a specified object. The function always returns True, unless the object is a falsy in Python.

Here is an instance:

Employees = {'Dave', 'Joey', 'Chandler'}
print('The bool value of the Employees set: ', bool(Employees))

Employees_ID = {}
print('The bool value of the Employees ID set: ', bool(Employees_ID))

Output:

The bool value of the Employees set:  True
The bool value of the Employees ID set:  False

You can refer to the screenshot below to see the output:

python check if set is empty

When we apply this to an empty set in Python, it will return False. As we know, an empty set is considered a falsy in Python.

Method 3. Use the Comparison Operator

The comparison operator(==) is used to compare two values in Python.

Here is the code to use the comparison operator to check if a Python set is empty:

Companies = {}
if len(Companies) == 0:
    print("The Companies set is empty.")
else:
    print("The Companies set is not empty.")

Technologies = {'Python', 'MySQL'}
if Technologies == set():
    print("The Technologies set is empty.")
else:
    print("The Technologies set is not empty.")

Output:

The Companies set is empty.
The Technologies set is not empty.

You can refer to the screenshot below to see the output:

check if set is empty python

We can compare our set to an empty set in Python or compare the length of the set to 0, as we know the length of the empty set is 0, to check if a set is empty or null in Python.

Method 4. Use Python’s if-else Conditional Statement

We can check for an empty Python set using the if-else condition statement. The if condition works when the statement passed to it is True; otherwise else works in Python.

Let’s see the example:

shops = {'Nike', 'Nautica', 'Dior', 'Gucci'}
if shops:
    print("The shops set is not empty.")
else:
    print("The shops set is empty.")

Factories = set()
if Factories:
    print("The Factories set is not empty.")
else:
    print("The Factories set is empty.")

Output:

The shops set is not empty.
The Factories set is empty.

You can refer to the screenshot below to see the output:

Empty Set in Python

We will directly pass our set to the if-else statement, and as we all know, an empty set is falsy in Python. So, when an empty set comes to the if-else, the else statement will work.

Method 5. Use the not Operator

We can reverse the answer of the if-else statement with the help of the not operator in Python and can print the statement accordingly.

The example is as follows:

shops = {'Nike', 'Nautica', 'Dior', 'Gucci'}
if not shops:
    print("The shops set is empty.")
else:
    print("The shops set is not empty.")

Factories = set()
if not Factories:
    print("The Factories set is empty.")
else:
    print("The Factories set is not empty.")

Output:

The shops set is not empty.
The Factories set is empty.

You can refer to the screenshot below to see the output:

how to know if a set is empty in Python

The not operator provides a simple way to check if a set is empty or not in Python.

Method 6. Use any() function in Python

We can directly apply the any() function to the set to check if the det is empty or not. If the set contains any element, the any() function will return True; otherwise, False in Python.

Here is an instance to check if a Python set is empty with the help of any() function:

Candies = {"M&Ms", "Reese’s", "Hershey Bar"}
print(any(Candies))

Toys = set()
print(any(Toys))

Output:

True
False

You can refer to the screenshot below to see the output:

Python check if set is empty or null

Python’s any() function quickly checks if a set has elements, returning True for non-empty sets and False for empty ones.

In this article, I have explained how to check if a Python set is empty, using methods like the len function, the bool function in Python, the comparison operator, the if-else conditional statement, the not operator, and using any() function in Python.

But depending on the situation, you might prefer one of the other methods.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.