How to check if a variable is a dictionary in Python?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Checking whether a value is a dictionary in Python is simple once you decide what “dictionary” means in your code. Sometimes you need the exact built-in dict type, but often what you really need is any object that behaves like a mapping.
Decide Between Exact Type and Mapping Behavior
These are different questions:
- is the value exactly a
dict - does the value implement mapping behavior
- does the value support mutation as well as lookup
Those distinctions matter because Python has several mapping types, including defaultdict, OrderedDict, and custom classes that implement the mapping protocol.
If the second pair of checks matches your intent better than the first pair, then Mapping is the right abstraction.
Use isinstance in Most Application Code
For inputs such as JSON payloads, configuration objects, or decoded request bodies, isinstance(value, Mapping) is usually the best test.
This accepts ordinary dictionaries and mapping-like objects without forcing every caller to use one exact implementation.
If the code also needs mutation, tighten the check to MutableMapping instead of assuming every mapping can be written to.
Use Exact dict Checks Only When You Mean It
Sometimes exact identity really is the requirement. Maybe you are optimizing for a specific low-level behavior or deliberately rejecting subclasses.
That check is intentionally narrow. It will reject valid mapping types that happen not to be built-in dict objects.
Validate Nested Structures Carefully
Real validation logic usually involves nested dictionaries, not one isolated type check. It is better to validate step by step than to hide everything inside a long boolean expression.
This keeps error messages useful and makes the code easier to maintain.
Type Hints Help, but They Do Not Validate Input
Type hints improve readability and static analysis, but they do not stop invalid runtime input.
The hint tells readers what you want. It does not protect you if a caller passes a list or None. If the data crosses a network, file, or user boundary, keep runtime validation in place.
Common Pitfalls
The most common mistake is using type(value) == dict by default and accidentally rejecting perfectly valid mapping types.
Another common issue is trying to detect dictionary-like objects with ad hoc checks such as looking for a keys attribute. Python already has mapping abstractions for this purpose. Developers also sometimes assume type hints enforce runtime behavior automatically, which they do not.
Summary
- Decide whether you need exact
dictidentity or general mapping behavior. - Prefer
isinstance(value, Mapping)for most application-level checks. - Use
type(value) is dictonly when rejecting subclasses is intentional. - Use
MutableMappingif your code requires writes as well as reads. - Keep runtime validation even when type hints are present.
Related reading
- How to check if a view controller is presented modally or pushed on a navigation stack?
- How to check if AlarmManager already has an alarm set?
- How to check if all elements of a list match a condition?
- How to check if an element is in an array
- How to check if an integer is a power of 3?
- How to check if an object is a generator object in Python?
- How to check if object is an array of a certain type?
- How to check if one of the following items is in a list?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.