Python Dictionary Contains Value: How to Check
Check whether a Python dictionary contains a specific value using `in` with `values()`, understand NaN and equality edge cases, and know when to use sets or reverse mappings for repeated lookups.

When you need to know whether a Python dictionary contains a value rather than a key, the standard membership test on keys does not apply. The phrase python dictionary contains value usually means checking if any of the dictionary's values equals a target object. This is a common operation in data validation, configuration checks, and deduplication logic. The direct approach is to use the in operator on the values() view.
The Basic Check: value in dict.values()
The simplest way to test for a value is to use the in operator on the dictionary's values() view. This view is a live, dynamic object that reflects the current contents of the dictionary. The membership test iterates through all values and compares each one to the target using equality (==).
scores = {"alice": 85, "bob": 92, "carol": 78}
print(92 in scores.values()) # True
print(100 in scores.values()) # FalseThis works because dict_values implements __contains__, which performs a linear scan. The time complexity is O(n) for each check, where n is the number of entries in the dictionary. For small dictionaries or infrequent checks, this is perfectly acceptable.
Unhashable Values and Equality Semantics
Unlike dictionary keys, which must be hashable, values can be any Python object, including lists, dictionaries, or custom class instances. The membership test on values() does not rely on hashing; it uses equality comparison. Therefore, unhashable values work without issue.
data = {"a": [1, 2], "b": [3, 4]}
print([1, 2] in data.values()) # TrueHowever, the behavior depends on how equality is defined for the objects involved. For built-in types like lists, == compares element-wise. For custom objects, the __eq__ method determines the result. If __eq__ is not implemented, identity is used, which may not match your intent.
A subtle pitfall is float('nan'). Since NaN is not equal to itself, float('nan') in vals.values() returns False even if the dictionary contains a NaN value.
vals = {"a": float('nan')}
print(float('nan') in vals.values()) # FalseIf you need to detect NaN, use a custom predicate with any() and math.isnan().
Performance: When O(n) Becomes a Problem
Repeatedly checking for a value in a large dictionary can become a bottleneck. Each in operation scans the entire values collection. If you perform this check frequently, consider a data structure that supports O(1) membership tests.
The following table summarizes common approaches and their tradeoffs.
| Approach | Time Complexity per Check | Best Use Case |
|---|---|---|
in dict.values() | O(n) | One-off checks, small dictionaries |
Maintain a set of values | O(1) average | Frequent checks, hashable values |
| Reverse dictionary | O(1) | Need to map value to key, unique or grouped values |
Maintaining a separate set of values requires updating it whenever the dictionary changes. This adds overhead to insertions and deletions but pays off when value lookups dominate. The set must contain only hashable values; if your values are lists or other unhashable types, you cannot use this approach directly.
Using any() for Complex Conditions
When you need to check whether any value satisfies a condition more complex than simple equality, use the built-in any() function with a generator expression. This is still O(n), but it gives you full control over the predicate.
data = {"x": 10, "y": 20, "z": 30}
print(any(v > 25 for v in data.values())) # TrueFor simple equality, in is more readable and slightly faster because it avoids the generator overhead. Reserve any() for cases where the condition is not a direct == comparison, such as type checks, range checks, or custom logic.
Handling Duplicate Values
Dictionaries can have duplicate values. The in operator returns True if at least one value matches the target. If you need to know how many times a value appears, you can convert the values to a list and use count(), but that creates a full list and is O(n). A more efficient approach for repeated counting is to use collections.Counter on the values.
from collections import Counter
scores = {"a": 1, "b": 2, "c": 1}
counter = Counter(scores.values())
print(counter[1]) # 2This is useful when you need frequency information, not just a boolean check. Because Counter stores elements as keys, the values must be hashable; lists or other unhashable values will raise a TypeError.
Building a Reverse Dictionary for Repeated Lookups
If you frequently need to find the key(s) associated with a given value, consider building a reverse mapping. This is particularly effective when the dictionary is static or changes infrequently. Because values may not be unique, the reverse mapping should store lists of keys. This also requires the values to be hashable, since they become keys in the reverse mapping.
original = {"a": 1, "b": 2, "c": 1}
reverse = {}
for key, value in original.items():
reverse.setdefault(value, []).append(key)
print(reverse) # {1: ['a', 'c'], 2: ['b']}Now checking 1 in reverse is O(1), and you can also retrieve all keys that map to that value. The cost is the initial O(n) build and extra memory. This pattern is worth using when the reverse lookup is performed many times and the dictionary is not mutated frequently.
Edge Cases: Custom Equality and NaN
As mentioned earlier, the membership test relies on ==. For custom objects, ensure that __eq__ is implemented correctly. If you rely on identity, two distinct objects with the same logical content will not match.
Another edge case is the presence of None. None in dict.values() works as expected because None == None is True. However, if your dictionary contains both None and other falsy values, the check is still unambiguous.
Finally, remember that dict.values() returns a view that reflects the current state. If you modify the dictionary after creating the view, the view updates accordingly. This is usually what you want, but be aware if you need a snapshot, convert it to a list first.
Understanding these nuances ensures that your value membership checks behave predictably in production, especially when dealing with complex data types or large datasets.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.