How to Check if a Key Exists in a Dictionary in Python

Learn the simplest ways to check whether a dictionary contains a key in Python. This page focuses on practical lookup patterns beginners use most often.

Quick answer

user = {"name": "Sam", "age": 25}

if "name" in user:
    print("Key exists")
else:
    print("Key does not exist")

Use the in operator for the clearest and most common solution.

What this page helps you do

  • Check whether a dictionary has a specific key
  • Avoid KeyError when reading dictionary values
  • Choose between in, get(), and try-except for different situations

Best method for most cases: use in

In Python, the best way to check whether a dictionary contains a key is:

key in my_dict

It returns:

  • True if the key exists
  • False if the key does not exist

This works especially well inside an if statement.

user = {"name": "Sam", "age": 25}

if "age" in user:
    print("The age key exists")

Output:

The age key exists

You can then safely read the value after checking:

user = {"name": "Sam", "age": 25}

if "name" in user:
    print(user["name"])

Output:

Sam

If you are still learning how dictionaries work, see Python dictionaries explained.

Using not in for missing keys

Use not in when you want to handle the missing case first.

user = {"name": "Sam"}

if "email" not in user:
    print("Email is missing")

Output:

Email is missing

This is useful when validating data before adding or updating it.

user = {"name": "Sam"}

if "age" not in user:
    user["age"] = 0

print(user)

Output:

{'name': 'Sam', 'age': 0}

This style is often easy to read in guard clauses because it handles the problem early. If you want to update dictionaries, see how to add a key to a dictionary in Python.

Use get() when you also want the value

The get() method returns the value for a key if it exists.

If the key is missing, it returns None by default instead of raising an error.

user = {"name": "Sam", "age": 25}

print(user.get("name"))
print(user.get("email"))

Output:

Sam
None

You can also provide your own default value:

user = {"name": "Sam", "age": 25}

email = user.get("email", "No email found")
print(email)

Output:

No email found

This is useful when you want to continue safely without getting a KeyError.

If you want a full explanation of this method, see the Python dictionary get() method. If your goal is reading values safely, you may also want how to access values in a dictionary in Python.

Know the difference between a missing key and a None value

A key can exist in a dictionary and still store None.

That means get() returning None does not always mean the key is missing.

user = {"name": "Sam", "email": None}

print(user.get("email"))
print(user.get("phone"))

Output:

None
None

Both lines return None, but they mean different things:

  • "email" exists and its value is None
  • "phone" does not exist at all

If you need to confirm that the key itself exists, use in:

user = {"name": "Sam", "email": None}

print("email" in user)
print("phone" in user)

Output:

True
False

When try-except makes sense

You can also access the key directly and catch the error if it is missing.

user = {"name": "Sam"}

try:
    print(user["email"])
except KeyError:
    print("The key does not exist")

Output:

The key does not exist

This works because reading a missing key with my_dict[key] raises a KeyError.

Use this approach when you want to try the access directly and handle failure. For most beginners, checking with in first is usually easier to read.

If you are seeing this error already, read KeyError in Python: causes and fixes or KeyError when accessing dictionary values.

Common beginner mistakes

Here are some common problems when checking dictionary keys.

1. Checking values instead of keys by accident

In a dictionary, in checks keys, not values.

user = {"name": "Sam", "age": 25}

print("name" in user)
print("Sam" in user)

Output:

True
False

If you want to check values, use .values():

print("Sam" in user.values())

2. Using get() to test existence when None may be a real value

This can cause confusion:

user = {"email": None}

if user.get("email") is None:
    print("This does not prove the key is missing")

The key may exist and still have the value None.

3. Accessing my_dict[key] before checking if the key exists

This can raise an error:

user = {"name": "Sam"}

print(user["email"])

This raises a KeyError because "email" is not in the dictionary.

4. Assuming in checks values in a dictionary

This is a very common mistake for beginners.

user = {"name": "Sam"}

print("Sam" in user)

This returns False because "Sam" is a value, not a key.

Common causes

These are the most common reasons beginners run into problems here:

  • Trying to read my_dict[key] when the key is missing
  • Confusing dictionary keys with dictionary values
  • Using get() without understanding its default return value
  • Expecting in to search values instead of keys

Helpful debugging checks

If your dictionary lookup is not working as expected, these quick checks can help:

print(my_dict)
print(my_dict.keys())
print("name" in my_dict)
print(my_dict.get("name"))
print(list(my_dict.items()))

What these show:

  • print(my_dict) shows the full dictionary
  • print(my_dict.keys()) shows all keys
  • print("name" in my_dict) tests one key directly
  • print(my_dict.get("name")) shows the value or None
  • print(list(my_dict.items())) shows key-value pairs

FAQ

Does in check keys or values in a dictionary?

It checks keys, not values.

How do I check if a value exists in a dictionary?

Use value in my_dict.values(). This is different from checking keys.

Should I use get() or in?

Use in when you only want to know if the key exists. Use get() when you want the value and a safe default.

Why did get() return None even though I expected a value?

The key may be missing, or the key may exist with None stored as its value.

See also