How to check if a dictionary is empty in Python

Dictionary is an unordered collection of items that stores the data in the form of key-value pairs. They are useful when the data has a unique reference, especially in database management. In this tutorial, we’ll discuss how to check if a dictionary is empty or not in python. We will discuss three methods to determine if a Dictionary is empty in Python.

Method 1: using if-else

The basic method is to use if-else statements. If the dictionary is empty, the if condition becomes true and the program will print a message that “dictionary is empty” otherwise display a message of “not empty”. Consider an example given below.

Example 1:

# Using IF-ELSE Method

fruits= {}

#Check if the dictionary is empty or not
if fruits:

   print("Dictionary of fruits is not empty")

else:

   print("Dictionary of fruits is empty")

Dictionary of fruits is empty

If the dictionary has elements, then the if-condition will become true and the statements inside the if-block will get executed. In this example, the dictionary of fruits is empty, therefore the compiler will execute statements inside the else block. For a better understanding of if-else statements, read this article.

You can also use a not operator with if statement as shown in the example below. Now if the dictionary is empty the compiler will execute if block. This code will behave opposite to the previous example.

Example 2:

fruits= {}

#Check if the dictionary is empty or not
if not fruits:

   print("Dictionary of fruits is empty")

else:

   print("Dictionary of fruits is not empty")

Dictionary of fruits is empty

Both of the programs are almost similar. Example 1 executes else block when the dictionary is empty and the program in example 2 executes if-block when the dictionary is empty.

METHOD 2: USING BOOL() Function

You can also use a bool function to check whether the dictionary is empty or not. If the dictionary has elements, the bool function will return False and vice versa.

Example 3:

books = {1:"Science",2:"Mathematics",3:"English", 4:"Arts"}

fruits= {}

print(bool(books))

print(bool(fruits))
True
False

You can also use a bool function to check along with if-else statements. Here, the bool() function will evaluate the condition. If the condition is satisfied, the bool function returns true and executes all the statements inside the if-block. If the Boolean expression evaluates to False, then else block will get executed.

Example 4:

books = {1:"Science",2:"Mathematics",3:"English", 4:"Arts"}

# Check if dictionary is empty
if bool(books):

   print("Dictionary of books is not empty")

else:

   print("Dictionary of books is empty")
Dictionary of books is not empty

Method 3: using len() function

Another possible method is to use the len() function. If the dictionary is empty, its length would be zero, and len() function would return 0.

Example 5:

The following example shows how to determine whether the list is empty or not using len() function.

fruits= {}

if len(fruits)==0:

   print("Dictionary of fruits is empty")

else:

   print("Dictionary of fruits is not empty")
Dictionary of fruits is empty.

You often come up with situations where you have to perform a certain operation on dictionaries. Before performing the operation, you first need to check whether the dictionary is empty or not. If the dictionary is empty, then initialize it first otherwise perform the desired operation. This article discussed different ways to check whether the dictionary is empty or not. If you have any queries regarding this article, contact us. Your feedback would be highly appreciated.

Leave a Comment

Your email address will not be published. Required fields are marked *