w3resource

Python: Check a dictionary is empty or not


18. Check if a Dictionary is Empty

Write a Python program to check if a dictionary is empty or not.

Sample Solution:

Python Code:

# Create an empty dictionary 'my_dict'.
my_dict = {}

# Check if the dictionary is empty by evaluating its boolean value.
# The 'bool()' function returns False for an empty dictionary, so if it's not empty, the condition is True.
if not bool(my_dict):
    # Print a message indicating that the dictionary is empty.
    print("Dictionary is empty")

Sample Output:

Dictionary is empty 

For more Practice: Solve these Related Problems:

  • Write a Python program to check whether a dictionary is empty by evaluating its boolean value.
  • Write a Python program to implement a function that returns True if a dictionary has no keys and False otherwise.
  • Write a Python program to test the emptiness of a dictionary by comparing its length to zero.
  • Write a Python program to use conditional expressions to output whether a dictionary is empty.

Go to:


Previous: Write a Python program to remove duplicates from Dictionary.
Next: Write a Python program to combine two dictionary adding values for common keys.

Python Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.