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.
Python3
# 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
The time complexity of this program is O(1), as it involves a constant number of operations.
The auxiliary space complexity of this program is also O(1), as it only uses a constant amount of additional memory to store the dictionary and the result variable.
Python | Check if dictionary is empty
Sometimes, we need to check if a particular dictionary is empty or not. In the web development domain in which we sometimes need to test for results of a particular query or check whether we have any key to add info into a database. Let’s discuss certain ways in which this task can be performed in Python.
Contact Us