How to check if the dictionary is empty in Python?
What is a dictionary in Python?
A directory is a collection of data but data is not ordered. Unlike other data types, it does not hold a single value as its element, it holds elements in the format of key-value pair, where keys and values are separated by ‘:’ and two keys are separated by a comma. A dictionary can have identical values but it cannot store identical keys.
While creating a dictionary we should keep in mind that keys are case sensitive i.e., both ‘day’ and ‘Day’ will be considered as different keys as the first letter of both words is in a different case (small letter and capital letter).
Example 1:
# Creating an empty Dictionary
dic1={}
print("Example of an empty dictionary: \n ")
print(dic1,"\n”)
# Creating a Dictionary with different keys
dic1={'name': 'Amit', 1: [8, 1, 20, 4]}
print("A normal dictionary using different Keys: \n ")
print(dic1)
Output:
Example of an empty dictionary:
{}
A normal dictionary using different Keys:
{'name': 'Amit', 1: [8, 1, 20, 4]}
Using the bool() function to check if a dictionary is empty
To perform this task, we use the bool function. This function converts the passed object to a Boolean value, but it is only possible if and only if the string is not empty. If an empty string is passed to this function, it returns false, as an empty string cannot be converted.
Example 2:
# Initializing empty dictionary
dict1={}
# Printing original dictionary with all its contents
print("The original dictionary : \n "+str(dict))
# Using thebool() function
# to checkthe status of the dictionary
rslt=notbool(dict1)
# Returning status of the dictionary
print("\nStatus of dictionary : \n "+str(rslt))
nw_dict={‘Name’: 'Amit', ‘Place’: 'Goa', ‘Object’: 'Pen', ‘Year’: '2022'}
print("\nThe original dictionary : \n "+str(nw_dict))
# Using thebool() function
# to check the status of the dictionary
rslt=notbool(nw_dict)
# Returning status of the dictionary
print("\nStatus of dictionary : \n "+str(rslt))
Output:

Using the not operator to check the status of the dictionary
Not operator can also be used to check the status of the dictionary, after using this operator it returns true if the dictionary is found out to be empty.
Example 3:
# Initializing empty dictionary
dict1={}
# Printing original dictionary with all its contents
print("The original dictionary : \n"+str(dict1))
# Using the not operator
# to check the status of the dictionary
rslt=notdict1
# Returning status of the dictionary
print("\nIs dictionary empty ? : \n "+str(rslt))
nw_dict={‘Name’: 'Amit', ‘Place’: 'Goa', ‘Object’: 'Pen', ‘Year’: '2022'}
# Printing original dictionary with all its contents
print("\nThe original dictionary : \n "+str(nw_dict))
# Using the not operator
# to check the status of the dictionary
rslt=notnw_dict
# Returning status of the dictionary
print("\nIs dictionary empty ? : "+str(rslt))
Output:

Using the len() function to check the status of the dictionary:
The len() function is also used to check the status of the dictionary when the dictionary is passed to this function, it returns its length and compares it and hence returns the result as true or false.
Example 4:
# Initializing empty dictionary
dict1={}
# Printing original dictionary with all its contents
print("The original dictionary : \n "+str(dict1))
# Using the len() function
# to checkthe status of the dictionary
rslt=len(dict1) ==0
# Returning status of the dictionary
print("\nIs dictionary empty ? : \n "+str(rslt))
nw_dict={‘Name’: 'Amit', ‘Place’: 'Goa', ‘Object’: 'Pen', ‘Year’: '2022'}
# Printing original dictionary with all its contents
print("\nThe original dictionary : \n "+str(nw_dict))
# Using the len() function
# to check the status of the dictionary
rslt=len(nw_dict) ==0
# Returning status of the dictionary
print("\nIs dictionary empty ? : \n "+str(rslt))
Output:

Using the Equality operator to check the status of the dictionary
While using this method, we take an additional empty dictionary to compare it with the original one. And after comparing it, the result is returned as “True” or “False”.
Example 5:
# Initializing a dictionary check it against an empty dictionary
nw_dict={‘Name’: 'Amit', ‘Place’: 'Goa', ‘Object’: 'Pen', ‘Year’: '2022'}
# Initializing empty dictionary
tst_dict={}
# Printing original dictionary with all its contents
print("The original dictionary :\n "+str(nw_dict))
# Using equality operator
# to check the status of the dictionary
rslt=tst_dict==nw_dict
# Returning status of the dictionary
print("\nIs dictionary empty ? : "+str(rslt))
Output:
The original dictionary :
‘Name’: 'Amit', ‘Place’: 'Goa', ‘Object’: 'Pen', ‘Year’: '2022'
Is dictionary empty ? : False