October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Check Whether a Dictionary Is Empty in Python (and Why None Passes)

For a known dictionary, use `if not d` to test whether it is empty. If `None` and `{}` have different meanings, check `d is None` first.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If 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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.