How to check if a set is empty in Python? A set is a mutable data structure that stores a collection of unique items. To check if a set is empty or not, you can utilize various built-in functions and operators of Python. In Python, an empty set is considered as False hence, when you pass an empty set to the bool() function it returns False, you can use bool() with an if condition to check if the set is empty.
You can check if the set is empty using many ways, for example, by using set(), len(), not operator, and bool() functions. In this article, I will explain how to check if the set is empty or not by using all these functions with examples.
1. Quick Examples of Checking If the Set is Empty
If you are in a hurry, below are some quick examples of checking if the set is empty.
# Quick examples of checking if python set is empty
# Example 1: Empty set in Python
empty_set = set()
# Example 2: Empty set
empty_set = {}
# Example 3: Using len() Function
myset = set()
if len(myset):
print("The set is empty")
else:
print("The set is not empty")
# Example 4: Check if Set Is Empty
# Using len() Function
myset = set()
if len(myset) == 0:
print("The set is empty")
else:
print("The set is not empty")
# Example 5: Using not operator
myset = set()
if not myset:
print("The set is empty")
else:
print("The set is not empty")
# Example 6: Comparing with another empty set
myset = set()
another_set = set()
if myset == another_set:
print("The set is empty")
else:
print("The set is not empty")
# Example 7: Using the bool() function
myset = set()
if not bool(myset):
print("The set is empty")
else:
print("The set is not empty")
# Example 8: Using set() method
myset = set()
if myset == set():
print("The set is empty")
else:
print("The set is not empty")
2. Create Empty Set
In Python, an empty set is a set that does not contain any elements. It is represented by the set() function or by using curly braces {} without any elements inside. For example, Both methods will create an empty set. However, it’s important to note that using curly braces {} without any elements inside will create an empty dictionary, not an empty set. To create an empty set explicitly, it’s recommended to use the set() function.
# Create empty set
empty_set = set()
print(empty_set)
# Output:
# set()
# Create empty set
empty_set = {}
print(empty_set)
# Output:
# {}
3. Check If the Set is Empty Using len() Function
You can use the len() function to check if a set is empty. Python len() function returns the number of elements in a collection. If the length of the set is 0, it means the set is empty.
In the below example, you can initialize an empty set called myset. The len() function is applied to myset in the if statement. If the length of the set is non-zero, the code inside the if block will execute, printing "The set is not empty". Otherwise, if the length is 0, the code inside the else block will execute, printing "The set is empty".
# Initializing an empty set
myset = set()
print("Given set:", myset)
# Using len() Function
if len(myset):
print("The set is not empty")
else:
print("The set is empty")
Yields below output.
Alternatively, you can also check the result len(myset) is equal to 0 to check if the set is empty. This is similar to the above example.
# Initializing an empty set
myset = set()
print("Given set:", myset)
# Check if Set Is Empty
# Using len() Function
if len(myset) == 0:
print("The set is empty")
else:
print("The set is not empty")
Yields below output.
In the above example, len(myset) returns the length of the set. If the length is zero, it means the set is empty, and the corresponding message is printed. If the length is non-zero, it means the set is not empty, and the else block is executed.
4. Check If the Set Is Empty Using the Not Operator
You can also check if a set is empty using the not operator in Python, you can simply apply the not operator to the set and evaluate the result. In the below example, you can create an empty set called myset. The not operator is applied to myset in the if statement. If the set is empty, the not operator will return True, and the code inside the if block will execute, printing “The set is empty”. Otherwise, if the set is not empty, the not operator will return False.
# Initializing an empty set
myset = set()
# Using not operator
if not myset:
print("The set is empty")
else:
print("The set is not empty")
# Output:
# The set is empty
5. Comparing with Another Empty Set
If you want to compare a set with another empty set to check if it is empty, you can use the == operator. For example, you have two sets, myset and another_set, both initialized as empty sets.
The == operator is used to compare the two sets. If they are equal (both empty), the code inside the if block will execute, printing "The set is empty". Otherwise, if they are not equal (at least one of them is not empty), the code inside the else block will execute, printing "The set is not empty".
# Initializing an empty sets
myset = set()
another_set = set()
# Comparing with another empty set
if myset == another_set:
print("The set is empty")
else:
print("The set is not empty")
# Output:
# The set is empty
6. Check if Set is Empty Using the bool() Function
You can also check if a set is empty using the bool() function in Python, you can simply pass the set as an argument to the bool() function. The bool() function returns False if the set is empty and True if it contains any elements.
In the below example, you create an empty set myset. We pass myset as an argument to the bool() function in the if statement. If the bool() function returns False, it means the set is empty, and the code inside the if block will execute printing "The set is empty". Otherwise, if the bool() function returns True, the code inside the else block will execute, printing "The set is not empty".
# Initializing an empty set
myset = set()
# Using the bool() function
if not bool(myset):
print("The set is empty")
else:
print("The set is not empty")
# Output:
# The set is empty
7. Check if the Set is Empty Using set() Method
You can also check if a set is empty using the set() method in Python, you can create an empty set and compare it with your existing set using the == operator. If the two sets are equal, it means the set is empty. For instance, you create an empty set myset using the set() method. You then compare myset with the set() using the == operator. If they are equal, the code inside the if block will execute, printing "The set is empty". Otherwise, if they are not equal, the code inside the else block will execute, printing "The set is not empty".
# Initializing an empty set
myset = set()
# Using set() method
if myset == set():
print("The set is empty")
else:
print("The set is not empty")
# Output:
# The set is empty
Conclusion
In this article, I have explained how to check if the set is empty in Python by using the set(), len(), not operator, and bool() functions with examples.
Happy Learning !!
Related Articles
- Python Ternary Conditional Operator
- Python set Operators
- Python String in operator with Examples
- Python String center() Method
- Get the First Element of a Tuple in Python
- Python String center() Method
- Python Not Equal Operator With Examples
- How to Check If a Python Dictionary is Empty
- Create a List of Tuples in Python
- How can I check for NaN values in Python?
- Python Global Variables in Function
- Python enumerate Explained with Examples
- Python if __name__ == “__main__”: Explain?
- Python Nested if else Statement Examples