Check if a Python Dictionary Contains a Key or Value

I was working on a project where I had to validate data stored in a Python dictionary. The challenge was simple: I needed to check if a dictionary contained specific keys or values.

At first, I thought there might be a built-in function like dict.contains(). But soon, I realized Python doesn’t have such a function. Instead, we use simple operators and methods.

In this tutorial, I’ll show you five easy ways to check if a Python dictionary contains a key or a value. I’ll also share practical examples from my experience so you can apply them right away.

Method 1 – Use Python in Operator to Check Keys

The most common way to check if a Python dictionary contains a key is by using the in operator. This is simple, clean, and works in almost every scenario.

# Dictionary of US states and their abbreviations
states = {
    "California": "CA",
    "Texas": "TX",
    "New York": "NY",
    "Florida": "FL"
}

# Check if 'Texas' exists as a key
if "Texas" in states:
    print("Yes, Texas is in the dictionary")
else:
    print("No, Texas is not in the dictionary")

I executed the above example code and added the screenshot below.

python dictionary contains

This method is my go-to choice because it’s readable and efficient. I often use it when validating user input against a predefined dictionary.

Method 2 – Use Python’s get() Method

Sometimes, you want to check if a key exists and avoid errors if it doesn’t. That’s where the get() method is super useful.

states = {
    "California": "CA",
    "Texas": "TX",
    "New York": "NY",
    "Florida": "FL"
}

# Using get() to check
result = states.get("Nevada")

if result:
    print("Nevada exists:", result)
else:
    print("Nevada does not exist in the dictionary")

I executed the above example code and added the screenshot below.

python dict contains key

I like get() because it avoids KeyError. This is especially helpful when working with APIs or large datasets where missing keys are common.

Method 3 – Check if a Value Exists with in and values()

Sometimes, you don’t care about the key but want to check if a value exists. For example, checking if “NY” exists in the dictionary values.

states = {
    "California": "CA",
    "Texas": "TX",
    "New York": "NY",
    "Florida": "FL"
}

# Check if a value exists
if "NY" in states.values():
    print("Yes, NY is in the dictionary values")
else:
    print("No, NY is not in the dictionary values")

I executed the above example code and added the screenshot below.

python dictionary contains key

This is a bit slower than checking keys, but it’s very handy. I use this when validating codes, abbreviations, or IDs stored as dictionary values.

Method 4 – Use keys() for Explicit Key Checking

If you want to make your code more explicit, you can use the keys() method. This makes it clear to readers that you’re only checking keys.

states = {
    "California": "CA",
    "Texas": "TX",
    "New York": "NY",
    "Florida": "FL"
}

if "California" in states.keys():
    print("California exists in the dictionary keys")
else:
    print("California not found")

I executed the above example code and added the screenshot below.

python dict contains

Although functionally the same as in, I sometimes use keys() for clarity. It helps other developers understand my intent quickly.

Method 5 – Use a Custom Function for Reusability

When I need to check dictionary contents multiple times in a project, I prefer writing a small reusable function.

def dict_contains(d, key=None, value=None):
    if key is not None:
        return key in d
    if value is not None:
        return value in d.values()
    return False

# Dictionary of states
states = {
    "California": "CA",
    "Texas": "TX",
    "New York": "NY",
    "Florida": "FL"
}

# Check key
print(dict_contains(states, key="Texas"))   # True

# Check value
print(dict_contains(states, value="CA"))   # True

# Check missing
print(dict_contains(states, key="Nevada")) # False

This function makes the code cleaner and avoids repeating logic. I often use it when writing utility modules for bigger Python applications.

Bonus Tip – Use any() for Partial Matches

Sometimes, you don’t want an exact match but want to check if a dictionary contains something similar.
For example, checking if any state abbreviation starts with “C”.

states = {
    "California": "CA",
    "Texas": "TX",
    "New York": "NY",
    "Florida": "FL"
}

# Check if any value starts with 'C'
if any(val.startswith("C") for val in states.values()):
    print("Yes, there is a value starting with C")
else:
    print("No value starts with C")

This is powerful when working with dynamic data. I’ve used it in projects where code followed certain patterns.

Practical Example – Check User Input Against a Dictionary

Let me share a real-world example. Suppose you’re building a small Python program that maps US state names to abbreviations.

# Example: Real-world use case of checking dictionary contains

states = {
    "California": "CA",
    "Texas": "TX",
    "New York": "NY",
    "Florida": "FL"
}

# User input
user_input = input("Enter a US state name: ")

if user_input in states:
    print(f"The abbreviation for {user_input} is {states[user_input]}")
else:
    print(f"Sorry, {user_input} is not in the dictionary")

This is a practical way of applying what we learned. I’ve used similar logic in form validation and data cleaning tasks.

Key Takeaways

  • Use the in operator for quick key checks.
  • Use get() when you want to avoid errors.
  • Use values() when checking dictionary values.
  • Use keys() for explicit clarity.
  • Write custom functions for reusable checks.

When I started working with Python dictionaries years ago, I often overcomplicated things.
Now I know the simplest methods, like in and get(), are usually the best.

So, the next time you need to check if a Python dictionary contains a key or value,
You can confidently use one of these five methods.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.