For a value you know is a dictionary, use if not my_dict: to check whether it is empty. An empty dictionary is false in a condition. But None is false too, so if those cases mean different things, test my_dict is None first.
Check whether a dictionary is empty
Python treats an empty dictionary as false in a condition, so if not d: runs when d is empty:
settings = {}
if not settings:
print("settings is empty")
To check whether it has at least one entry, use if d:. These are truth-value checks: Python considers an object false when its __bool__() method returns False, or, if it has no __bool__(), when its __len__() method returns zero. The Python standard types documentation lists an empty dictionary, {}, as false.
Why does if not d also catch None?
None is a false constant in Python, not an empty dictionary. As a result, if not value: also matches None, as well as other false values such as False, numeric zero, and empty strings or lists. Truthiness alone tells you that a value is false; it does not identify which false value it is.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →#1 Best Overall
Distinguish None from an empty dictionary
If None means no dictionary was supplied while {} means a dictionary was supplied with no entries, check for None separately:
settings = None
if settings is None:
print("settings was not provided")
elif not settings:
print("settings was provided but is empty")
else:
print("settings has entries")
Use is None for this specific check. Python’s is operator tests object identity, and None is a singleton, so this is the idiomatic way to ask whether a value is None.
Rank #2
When to use len(d) == 0
len(d) == 0 is an explicit alternative for a known dictionary. Use it when the length itself is central to the surrounding code or explanation; otherwise, if not d: is usually the shorter emptiness check.
What if the value might not be a dictionary?
if not d: does not verify that d is a dictionary. It is a general truth-value test, so an empty list, zero, False, or None can enter the same branch. If the input type is uncertain, validate it or handle the types your code expects explicitly. For example, when only dictionaries and None are valid, test for None first and then check the dictionary’s truth value.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitchesIf you need to create an empty dictionary, use {} or call dict() with no positional argument. The standard documentation for dictionary objects describes dict() as creating an empty dictionary in that case.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




