Use key in dictionary to check whether a Python dictionary contains a key. It returns True or False and checks keys, not values. Choose get() when you want a fallback value, or bracket lookup when a missing key should raise an error.
Check for a key with in
Python’s built-in mapping membership test is key in d. It returns True if the dictionary has that key and False otherwise, as documented in the Python 3.14.8 built-in types reference.
settings = {"theme": "dark", "timeout": None}
if "theme" in settings:
print("theme is present")
if "timeout" in settings:
print("timeout is present, even though its value is None")
The test checks for keys. To check whether a value appears in a dictionary, use value in d.values() instead.
Choose between in, get(), and bracket lookup
| What you need | Use | What happens if the key is missing |
|---|---|---|
| Test presence and branch on the result | key in d |
Returns False |
| Read a value and allow it to be absent | d.get(key, default) |
Returns the supplied default; if omitted, returns None |
| Read a required value | d[key] |
Raises KeyError for a missing key in an ordinary dictionary |
The Python reference documents these behaviors for in, get(), and subscription in its dictionary documentation. Use the operation whose missing-key behavior matches what your code should do.
#1 Best Overall
Use get() for a fallback
For example, this uses 3 when the retries key is absent:
retries = settings.get("retries", 3)
If you leave out the second argument, get() returns None when the key is missing. Unlike d[key], it does not raise KeyError for that case.
Rank #2
Use brackets when the value is required
Use d[key] when missing data should be treated as an error. In an ordinary dictionary, requesting an absent key raises KeyError, which can expose invalid or incomplete input rather than silently substituting a fallback.
Distinguish a missing key from a value of None
settings.get("timeout") returns None both when "timeout" is absent and when it is present with the value None. If those cases mean different things, test membership separately:
Free tools Windows power users keep installed
One-click scans. No signup required.
if "timeout" in settings:
timeout = settings["timeout"] # The value may intentionally be None.
else:
timeout = 30
Alternatively, give get() a unique sentinel so a stored None is distinguishable from absence:
missing = object()
timeout = settings.get("timeout", missing)
if timeout is missing:
timeout = 30
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Keep dictionary key behavior in mind
Keys must be hashable
Dictionary keys must be hashable. Common immutable values such as strings and numbers can be keys; lists and dictionaries cannot normally be used as keys. See the Python built-in types reference.
Equal keys can refer to the same entry
Python treats values that compare equal as interchangeable dictionary keys. For example, 1, 1.0, and True can address the same entry. This behavior is described in the Python 3.14.7 built-in types reference.
Insertion order does not affect membership
Dictionaries preserve insertion order, guaranteed starting with Python 3.7. That ordering affects iteration, not whether key in d finds a key; the Python documentation treats these as separate behaviors.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteBest Value
Custom dictionary subclasses may alter bracket lookup
A dict subclass can define __missing__(), which may be called by bracket lookup when a key is absent. The Python reference notes that other operations do not invoke __missing__(). For a custom mapping or subclass, check its documented behavior rather than assuming every lookup acts exactly like an ordinary dictionary; see the reference for dictionary types.
Quick Recap
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.




