In this article, I will discuss how you can check if Set is Empty in Python. If you have been using Set recently to have a list of unique characters then you might know that Python does not have an inbuilt function to check if Set is empty.

In this tutorial, you will learn how you can check If Set is empty in various ways. I will show you different ways with code examples that you can use in your Program to verify an Empty Set in Python.

Let us see the various ways with code examples below.

## 1. Using If Operator To Check If Set Is Empty In Python

The first method that you can use is the easiest method. If a set is empty and you are using the if condition before the set variable then it will return 0 if the set is empty indicating the if operator that this condition is false, and the set is empty.

Otherwise, if the set is not empty then using the if operator it will return some number that will indicate that it is true and the set is not empty.

Let us see in the below example code the usage of if condition to Check If Set is Empty.

#Initializing the Empty Set newSet = set() #Using the boolean Function to check if #Set is Empty if newSet: print("Set is not Empty.") else: print("Set is Empty.")

**Output:**

`Set is Empty.`

As you can see in the above code, the boolean if operator checks if the new set variable return 0 if it is empty indicating the if condition to be false and giving the output as the set is empty.

Let us check the same above code when the set is not empty does it provide the desired output or not. So to check It I will be adding an element to the set and then executing the code and check.

#Initializing the Empty Set newSet = set() newSet.add(1) #Using the boolean Function to check if #Set is Empty if newSet: print("Set is not Empty.") else: print("Set is Empty.")

**Output:**

`Set is not Empty.`

## 2. Using len Function To Check If Set Is Empty in Python

The second method is using the len() function of the Python. This method returns the number of items present in the set. If the input set contains two elements in it then it will return 0 and then we can use the len() function with the if condition to make sure Set is Empty.

Let us see in the below example code the usage of len() function for Empty Set Literal in Python.

#Initializing the Empty Set newSet = set() #Using len function to check #The Length of Set length = len(newSet) #Using the boolean Function to check if #Set is Empty if length: print("Set is not Empty. And Length of Set is {}".format(length)) else: print("Set is Empty. And Length of Set is {}".format(length))

**Output:**

`Set is Empty. And Length of Set is 0`

As you can see in the above code, The length of the input set is 0, and hence the if condition to check for an empty set sets to false, and else condition print statement becomes the output here.

Let us see if the above code provides the desired output when the input set is not empty.

#Initializing the Empty Set newSet = set() #Adding An Element to Set newSet.add(1) #Using len function to check #The Length of Set length = len(newSet) #Using the boolean Function to check if #Set is Empty if length: print("Set is not Empty. And Length of Set is {}".format(length)) else: print("Set is Empty. And Length of Set is {}".format(length))

**Output:**

`Set is not Empty. And Length of Set is 1`

## 3. Using bool Method To Check If Set is Empty In Python

The third method that you can use is the bool^{[1]} function. This method checks if the bool is empty then set the boolean variable as False, else if the set is not empty then it set the boolean variable is True.

Let us see in the below code example the usage of the bool method to check Empty Set in Python.

#Initializing the Empty Set newSet = set() #Using the Bool Method #To Check if the Set is Empty boolVariable = bool(newSet) #Using the boolean Function to check if #Set is Empty if boolVariable: print("Set is not Empty.") else: print("Set is Empty.")

**Output**:

`Set is Empty.`

As you can see in the above code, the bool variable sets to false when the set is empty and hence making the else statement in the above code as true and hence you see the output to be set is empty.

Let us see if the same code shown above provides the proper output as Set is not empty when it is actually not empty.

#Initializing the Empty Set newSet = set() #Adding a new Element to the Set newSet.add(1) #Using the Bool Method #To Check if the Set is Empty boolVariable = bool(newSet) #Using the boolean Function to check if #Set is Empty if boolVariable: print("Set is not Empty.") else: print("Set is Empty.")

**Output:**

`Set is not Empty.`

## 4. Using the Equals Operator To Check If Set Is Empty In Python

The fourth method that you can use to check if the set is empty in Python is using the equals to the operator. The equals to operator combined with the new set variable return true if the set is empty otherwise it returns false if the set is non-empty.

Let us see in the below example code the usage of the equals to an operator to check if the set is empty in Python.

#Initializing the Empty Set newSet = set() #Using the equals to operator #To Check if set is empty if newSet == set(): print("Set is Empty.") else: print("Set is Not Empty.")

**Output:**

`Set is Empty.`

As you can see in the above code, equals to the operator with a set() function returns true if the set is empty, and hence this code prints out that the set is empty.

Let us see if the set is not empty is printed out when the set is having at least one element added to it.

#Initializing the Empty Set newSet = set() #Adding a new element to the set newSet.add(1) #Using the equals to operator #To Check if set is empty if newSet == set(): print("Set is Empty.") else: print("Set is Not Empty.")

**Output:**

`Set is Not Empty.`

## Wrap Up

I hope you were able to learn how to check if the set is empty in Python. I have listed down the four methods that you can use in your program with code examples.

Let me know in the comment section if you know any better method than the one discussed above I will be happy to add it here.

If you liked the above tutorial then please follow us on **Facebook **and **Twitter**. Let us know the questions and answer you want to cover in this blog.

**Further Read:**

- Code Examples – Write A List To CSV Using Python
- Code Example: Loop Back To The Beginning Of A Program
- How To Do Nothing In An If Statement Python
- Code Example: Remove The First Item From List Python
- 4 Ways To Loop Through List With Index Using Python
- Code Examples: Multiply In Python
- How To Join Words In a List Using Python Code Example
- Python Code Example: How To Find Element in a List in 6 Way
- How To Test For An Empty Object In JavaScript
- How To Iterate Over Rows in a DataFrame in Pandas Python

- How To Get First Key From Dictionary Python – 3 Methods - January 25, 2022
- How To Check If A Number Is Even In Python - January 25, 2022
- How To Change A Variable Outside Of A Function In Python - January 25, 2022