Python Practice › Dictionary Programs › Program 31
Is a dictionary empty?
Write a Python program to check whether a dictionary is empty.
Input
{}, {"a": 1}Output
{} -> empty
{'a': 1} -> not emptyApproach
- An empty dict is falsy.
Solution
for d in [{}, {"a": 1}]: print(d, "-> empty" if not d else "-> not empty")Output
{} -> empty
{'a': 1} -> not emptyLearn the concept
New to this topic? Read the lesson, then come back.