Check if a Dictionary is Empty using len()
Here, we are using the Python len() to check if the dictionary is empty or not.
Python3
# initializing empty dictionary test_dict = {} # printing original dictionary print ( "The original dictionary : " + str (test_dict)) # Check if dictionary is empty using len res = len (myDict) = = 0 # print result print ( "Is dictionary empty ? : " + str (res)) |
Output:
The original dictionary : {} Is dictionary empty ? : True
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.