Check if dictionary is empty in Python
Sometimes, we need to check if a particular dictionary is empty or not.
# initializing empty dictionary
d = {}
print(bool(d))
print(not bool(d))
d = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print(bool(d))
print(not bool(d))
Output
False True True False
Different Methods to Check if a Dictionary is Empty
Table of Content
- Different Methods to Check if a Dictionary is Empty
- Check if a Dictionary is Empty using bool()
- Check if a Dictionary is Empty using not operator
- Check if a Dictionary is Empty using len()
- Check if a Dictionary is Empty using the Equality Operator
- Check if a Dictionary is Empty using the Using the not operator with the __len__() method:
- Using reduce()
- Using heapq:
Check if a Dictionary is Empty using bool()
The bool function can be used to perform this particular task. As the name suggests it performs the task of converting an object to a boolean value, but here, passing an empty string returns a False, as a failure to convert something that is empty.
# initializing empty dictionary
test_dict = {}
# printing original dictionary
print("The original dictionary : " + str(test_dict))
# using bool()
# Check if dictionary is empty
res = not bool(test_dict)
# print result
print("Is dictionary empty ? : " + str(res))
Output
The original dictionary : {} Is dictionary empty ? : True
Check if a Dictionary is Empty using not operator
This task can also be performed using the not operator that checks for a dictionary existence, this evaluates to True if any key in the dictionary is not found.
# initializing empty dictionary
test_dict = {}
# printing original dictionary
print("The original dictionary : " + str(test_dict))
# using not operator
# Check if dictionary is empty
res = not test_dict
# print result
print("Is dictionary empty ? : " + str(res))
Output
The original dictionary : {} Is dictionary empty ? : True
Check if a Dictionary is Empty using len()
Here, we are using the Python len() to check if the dictionary is empty or not.
# initializing empty dictionary
d = {}
# printing original dictionary
print("The original dictionary : " + str(d))
# Check if dictionary is empty using len
res = (len(d) == 0)
# print result
print("Is dictionary empty ? : " + str(res))
Output
The original dictionary : {} Is dictionary empty ? : True
Check if a Dictionary is Empty using the Equality Operator
Here, we are comparing the dictionary with values with an empty dictionary to check if the dictionary is empty or not.
# initializing empty dictionary
myDict = {1: 'Hello', 2: 'World' }
test_dict = {}
# printing original dictionary
print("The original dictionary : " + str(myDict))
# using operator
# Check if dictionary is empty
res = test_dict == myDict
# print result
print("Is dictionary empty ? : " + str(res))
Output
The original dictionary : {1: 'Hello', 2: 'World'} Is dictionary empty ? : False
Check if a Dictionary is Empty using the Using the not operator with the __len__() method:
This approach is similar to the previous one, but it uses the __len__() method instead of the len() function to get the length of the dictionary.
# Initialize a dictionary with some key-value pairs
myDict = {1: 'Hello', 2: 'World' }
# Print the original dictionary
print("The original dictionary : " + str(myDict))
# Check if the dictionary is empty using the equality operator
res = myDict.__len__()==0
# Print the result
print("Is dictionary empty ? : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
Output
The original dictionary : {1: 'Hello', 2: 'World'} Is dictionary empty ? : False
Using reduce()
- Initialize an empty dictionary test_dict.
- Print the original dictionary.
- Use the reduce method to check if the dictionary is empty:
- Initialize the accumulator to True.
- Iterate over each key-value pair in the dictionary using reduce.
- Return False as soon as a key-value pair is encountered.
- If the iteration completes without finding any key-value pairs, return the initial value of the accumulator,
- which is True.
- Print the resulting boolean value.
from functools import reduce
# initializing empty dictionary
test_dict = {}
# printing original dictionary
print("The original dictionary : " + str(test_dict))
# using reduce method to check if dictionary is empty
res = reduce(lambda acc, x: False, test_dict, True)
# print result
print("Is dictionary empty ? : " + str(res))
#This code is contributed by Rayudu.
Output
The original dictionary : {} Is dictionary empty ? : True
Using heapq:
- Import the heapq module.
- Initialize an empty list.
- Check if the list is empty using the nsmallest() method of the heapq module with k=1 and a custom key
- function that returns 0 for all elements.
- If the smallest element is not found, then the list is empty.
- Return the result.
import heapq
# initialize empty list
test_list = []
# printing original dictionary
print("The original dictionary : " + str(test_list ))
# check if list is empty using heapq
res = not bool(heapq.nsmallest(1, test_list, key=lambda x: 0))
# print result
print("Is list empty? : " + str(res))
#This code is contributed by Rayudu.
Output
The original dictionary : [] Is list empty? : True