How to Check if a Python Set is Empty? [6 Ways]

In this Python article, I will explain how to check if a Python set is empty. I will use six different methods like comparison operator, len() function, etc to check if a set is empty in Python with the help of some examples.

An empty set in Python is nothing but a set that does not contain any element inside it. This is considered as Falsy in Python as well.

Here is the list of all the methods that can be used to check if a set is empty in Python:

  1. Using len() function
  2. Using bool() function
  3. comparison operator
  4. Using if-else conditional statements
  5. Using not operator
  6. Using any() function

Let’s see all of them with some illustrative examples:

1. Check if a Python set is empty using the len() function

The len() function in Python is used to find the length of an iterable. It returns the number of items present inside that iterables. For the set in Python, if the length is zero, it means that the set is empty in Python.

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: Here, we have taken two Python sets, one with elements and the other without anything. The 0 in the output represents the empty set in Python.

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

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

How to Check if a Python Set is Empty

2. Python check if a set is empty using the bool() function

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.

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.

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

Here is the screenshot:

python check if set is empty

3. Check if a Python set is empty using the comparison operator

The comparison operator(==) is used to compare two values in 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 is 0, to check if a set is empty or null 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.

Below is the screenshot taken after the code was executed in the Pycharm editor.

check if set is empty python

4. Check for an empty set in Python using an 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.

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.

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.

Here is the screenshot of the executed code in the Pycharm Python editor:

Empty Set in Python

5. Check if a set is empty in Python using 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.

The screenshot below presents the output after the code was successfully implemented in the Pycharm editor.

how to know if a set is empty in Python

6. Python check if set is empty using any() function

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

Upon executing the code in Pycharm, the resulting output is displayed in the screenshot below.

Python check if set is empty or null

Conclusion

There are multiple ways to check if a Python set is empty. The choice is ours whether we want to use the len() function, bool() function, if-else statements, comparison operator, not, or any() function. We have to what will best suit our program.

I hope you understand all the methods and will use them in your code whenever needed.

You may also like to read: