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.
To check if a dictionary is empty in python, four common methods are used. The first method is by using if-else function, it is a simple and very easy method. The second method includes using bool() Function. Similarly you can also use determine by using len() Function. The last method includes using any() Function to determine if the dictionary is empty or not.
use if-else to determine if a dictionary is empty in Python
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.
USe BOOL() Function t0 check if a dict is empty or not
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
use len() function to check if a dictionary is empty or not in python
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.
Using the any() function to check if dict is empty
The any function of Python accepts iterable i.e., list, tuple, dictionary etc. as an argument and returns a Boolean value based on it. It returns True if any element or item in an iterable is true and False if any item in an iterable is false. For an empty iterable object it returns False. So you can easily get to know if the dictionary is empty or not by using this any() Function. We can assume that any() function will return False for any empty dictionary.
# using any() function
emptydictionary = {}
if any(emptydictionary):
print('This dictionary is not empty')
else:
print('This dictionary is empty')
The dictionary 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.