
This error is a core Python concept. The TypeError unhashable type message means you tried to use something “changeable” (mutable) in a place that requires something “unchangeable” (immutable).
- Hashable (Immutable): Can be “locked” into a unique ID. Examples:
str,int,tuple. - Unhashable (Mutable): Can be changed in-place. Examples:
list,dict.
The Rule: Dictionary keys and Set items MUST be hashable.
The Cause 1: Dictionaries
You cannot use a list as a dictionary key. Python doesn’t allow it because if you changed the list later, the dictionary would break.
Problem Code:
my_list = [1, 2]
my_dict = {
my_list: "My Value" # CRASH!
}
# TypeError: unhashable type: 'list'The Fix: Use an immutable tuple instead. Tuples look just like lists but use () and cannot be changed.
my_tuple = (1, 2)
my_dict = {
my_tuple: "My Value" # Works!
}
print(my_dict[(1, 2)]) # Output: My ValueThe Cause 2: Sets
Sets are for storing unique, hashable items. You can’t put a list inside a set. Problem Code:
my_set = { [1, 2], [3, 4] } # CRASH!The Fix: Use tuples.
my_set = { (1, 2), (3, 4) } # Works!




