to check weather a dictionary is empty or not in python 1

to check weather a dictionary is empty or not in python

empty_dict = {}
 
if empty_dict:
    print('Dictionary is not empty!')
else:
    print('Dictionary is empty!')

Here is what the above code is Doing:
1. We create an empty dictionary.
2. We check if the dictionary is empty.
3. If the dictionary is empty, we print a message.
4. If the dictionary is not empty, we print a different message.

In this case, the dictionary is empty, so we print the message ‘Dictionary is empty!’.

Similar Posts