Last modified: Dec 06, 2025 By Alexander Williams
Fix TypeError: unhashable type 'dict' in Python
You see a TypeError in Python. It says unhashable type 'dict'. This is a common error. It confuses many beginners. This guide will explain it. You will learn how to fix it.
We will cover the error's cause. We will show practical solutions. You will get example code. You will also see the corrected output.
What Does This Error Mean?
The error occurs when you use a dictionary as a key. You might also use it in a set. Python requires keys to be hashable. Dictionaries are not hashable. They are mutable. Their contents can change.
Hashable objects have a fixed hash value. This value never changes. Integers and strings are hashable. Lists and dictionaries are not. This is a core Python rule.
Understanding Hashability in Python
Think of a hash as a unique fingerprint. Python uses it for fast lookups. It is used in sets and dictionary keys. The hash() function returns this value.
Mutable objects cannot have a stable hash. If an object changes, its hash would change. This would break the data structure. So Python prevents it.
# This works: hashable types
print(hash(42)) # Integer
print(hash("hello")) # String
print(hash((1, 2))) # Tuple (if all items are hashable)
# This fails: unhashable types
my_dict = {"a": 1}
print(hash(my_dict)) # This will raise TypeError
Output for hashable types (example values):
42
-762302913
-3550055125485641917
Traceback (most recent call last):
File "", line 7, in
TypeError: unhashable type: 'dict'
Common Scenarios Causing the Error
You will see this error in specific situations. The most common is using a dict as a key in another dict. Another is adding a dict to a set.
# Scenario 1: Using a dict as a dictionary key
my_dict_key = {"id": 123}
data = {my_dict_key: "value"} # ERROR!
# Scenario 2: Adding a dict to a set
my_set = set()
my_set.add({"name": "Alice"}) # ERROR!
# Scenario 3: Using dict in set operations
set_a = {1, 2, {"a": 1}} # ERROR!
All these examples will fail. They try to use a mutable dict where immutability is required.
How to Fix the TypeError
The solution is simple. Convert the dictionary to an immutable type. The best choice is often a tuple. You can also use a frozenset in some cases.
Convert the dictionary items to a tuple. This makes it hashable.
# Original error-causing code
bad_dict = {{"user": "john"}: "data"}
# Fixed code: Use tuple of sorted items
user_info = {"user": "john"}
# Convert dict items to a tuple
key_tuple = tuple(sorted(user_info.items()))
good_dict = {key_tuple: "data"}
print(good_dict)
Output:
{(('user', 'john'),): 'data'}
We used sorted() for consistency. It ensures the same tuple for the same data. Order does not matter in dicts.
Using a Frozenset for Unordered Keys
Sometimes order is not important. A frozenset is another immutable type. It is perfect for unordered collections.
# Using frozenset for dict items
config = {"mode": "test", "verbose": True}
immutable_key = frozenset(config.items())
my_set = set()
my_set.add(immutable_key) # This works!
print(my_set)
Output:
{frozenset({('verbose', True), ('mode', 'test')})}
Real-World Example: Deduplicating Dictionaries
Imagine you have a list of dictionaries. You want to remove duplicates. You cannot put dicts directly into a set. You must convert them first.
list_of_dicts = [
{"a": 1, "b": 2},
{"a": 1, "b": 2}, # Duplicate
{"c": 3},
{"a": 1, "b": 2} # Another duplicate
]
# Convert each dict to a tuple of sorted items
unique_tuples = {tuple(sorted(d.items())) for d in list_of_dicts}
# Convert back to list of dicts if needed
unique_dicts = [dict(t) for t in unique_tuples]
print(unique_dicts)
Output:
[{'a': 1, 'b': 2}, {'c': 3}]
This technique is very powerful. It is useful for data cleaning.
Related Python TypeErrors
Understanding hashability helps with other errors. For example, you might see Fix TypeError: 'set' object is not subscriptable. This is also about data structure rules.
Another common issue is Fix TypeError: 'method' object is not subscriptable. It involves calling syntax errors.
Also, learn about Fix TypeError: a bytes-like object is required, not 'str'. It deals with data type mismatches.
Best Practices to Avoid the Error
Always remember the rule. Dictionary keys must be hashable. Use simple types like strings or numbers. If you need a compound key, use a tuple.
Plan your data structures early. Ask: "Will I need to use this as a key?" If yes, design it to be immutable from the start.
Use tuples for multi-part keys. For example, use (user_id, date) instead of a dict.
Conclusion
The "unhashable type 'dict'" error is straightforward. It protects your data integrity. Python enforces hashability for good reasons.
The fix is to convert your dictionary. Use a tuple or a frozenset. This makes the data immutable and hashable.
You now understand the core concept. You can solve this error quickly. Remember to check for similar issues like 'set' object is not subscriptable. Keep coding and learning.