Is a dictionary empty?

EasyDictionary Programs · 31 of 94

Write a Python program to check whether a dictionary is empty.

Advertisement
Input
{}, {"a": 1}
Output
{} -> empty
{'a': 1} -> not empty

Approach

  1. An empty dict is falsy.

Solution

Python
for d in [{}, {"a": 1}]:    print(d, "-> empty" if not d else "-> not empty")
Output
{} -> empty
{'a': 1} -> not empty
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs