Register Login

Python: Check if Dictionary is Empty

Updated May 19, 2020

In this article, we will learn how to check whether a dictionary is empty or not. A Dictionary in python is an immutable sequence of characters. Python Dictionaries are written within curly brackets and consist of key and values. To check if a Dictionary is an empty or not we have many built-in function and operators.

We can check if Python dictionaty is empty using:

Let us understand it more briefly with the help of an example.

Example 1: Using the not Operator

# Initializing an empty Dictionary
MyDict = {}
# Using not operator
if not MyDict:
    print ("Dictionary is empty")
else:
    print ("Dictionary is not empty")

Output

Dictionary is empty

Explanation

In the above example, we created an empty Dictionary ‘MyDict’. Then we used a not operator to reverse the false value.

In python, an empty Dictionary always evaluates to false. So when we passed an empty Dictionary to the if condition it’ll be evaluated to false. But the not operator reverses the false value to true value.

Thus the if condition is set to true. And we got “Dictionary is empty” as an output.

Example 2: Using the len() Function

# Initializing an empty Dictionary
MyDict = {}
# Using len() function
Length_MyDict = len(MyDict)
# Using if-else Statement
if Length_MyDict == 0:
    print ("Dictionary is empty")
else:
print ("Dictionary is not empty")

Output

Dictionary is empty

Explanation

In the above example, at first, we initialized empty Dictionary ‘MyDict’. Then we used a built-in function len() to calculate the length of the Dictionary and stored it in the variable ‘Length_Dictionary ’. Then we used if statement to check if the length of the list equals to zero or not.

If the condition sets to be true then the Dictionary is empty. Otherwise, the Dictionary is not empty.   

Example 3: Comparing with Another Empty Dictionary

# Initializing an empty Dictionary ‘MyDict1’
MyDict1 = {1: 'Hello', 2: 'World' }
# Initializing an empty Dictionary ‘MyDict2’
MyDict2 = {}
# Comparing both the Dictionary
if MyDict1 == MyDict2:
print('The Dictionary is empty!')
else:
print('The Dictionary is not empty!')

Output

The Dictionary is not empty!

Explanation

In the above example, we initialized two Dictionary MyDict1 and MyDict2. Suppose we have to check for a Dictionary ‘MyDict1’ if it is empty or not. Then we can do so by initializing an empty Dictionary say ‘MyDict2’. And then comparing ‘MyDict1’ with ‘MyDict2’ using the decision making statement i.e if-else condition. If ‘MyDict1’ is equal to ‘MyDict2’ then that means ‘MyDict1’ is an empty Dictionary. Else it is not empty. And this is how we can check for an empty Dictionary by comparing it with another empty Dictionary.

Example 4: Creating an Empty Dictionary

To create an empty dictionary in python, we have a clear() method in python.

This method neither takes any parameter nor returns any value.

Example

# Initializing an empty Dictionary ‘MyDict’
MyDict = {1: 'Hello', 2: 'World' }
# Emptying the dictionary
MyDict.clear()
# Using not operator
if not MyDict:
    print ("Dictionary is empty")
else:
print ("Dictionary is not empty")

Output

Dictionary is empty

Explanation

In this example, we used clear() method to delete the items of the dictionary. And then checked using not operator if ‘MyDict’ is empty or not.

Note: clear() method only delete the elements of the dictionary. And not the whole of the dictionary.

Conclusion

In this article, we saw four different ways to check for an empty Dictionary. The three ways we discussed above are:

  • Using not
  • Using len()
  • Comparison with an empty Dictionary

Then we read about how to empty the dictionary using clear() method.


×