Solve - TypeError: unhashable type: 'list' (Python)


Borislav Hadzhiev

Wed Apr 20 20223 min read

Solve - TypeError: unhashable type: 'list' (Python) #

The Python "TypeError: unhashable type: 'list'" occurs when we use a list as a key in a dictionary or an element in a set. To solve the error, convert the list to a tuple, e.g. tuple(my_list) as list objects are mutable and unhashable.

typeerror unhashable type list

Here are 2 examples of how the error occurs.
# 👇️ using list as a key in dictionary # ⛔️ TypeError: unhashable type: 'list' my_dict = {'name': 'Alice', ['dev', 'test']: 'tasks'} # 👇️ using list as an element in a set # ⛔️ TypeError: unhashable type: 'list' my_set = {['a', 'b', 'c']}
We can't use a list as a key in a dictionary or as an element in a set because list objects are mutable and unhashable.

You can solve the error by converting the list to a tuple.
my_dict = {'name': 'Alice', tuple(['dev', 'test']): 'tasks'} print(my_dict) # 👉️ {'name': 'Alice', ('dev', 'test'): 'tasks'} # 👇️ same approach when accessing key print(my_dict[tuple(['dev', 'test'])]) # 👉️ tasks my_set = {tuple(['a', 'b', 'c'])} print(my_set) # 👉️ {('a', 'b', 'c')}

Tuple objects are immutable and are hashable.

You can also declare a tuple directly by wrapping the items in parenthesis and not square brackets.
my_tuple = ('a', 'b', 'c') print(my_tuple) # 👉️ ('a', 'b', 'c') print(type(my_tuple)) # 👉️ <class 'tuple'>

An alternative solution would be to convert the list to a JSON string.
import json # 👇️ convert list to JSON string my_json = json.dumps(['dev', 'test']) my_dict = {'name': 'Alice', my_json: 'tasks'} print(my_dict) # 👉️ {'name': 'Alice', '["dev", "test"]': 'tasks'} # 👇️ use JSON string for key lookup print(my_dict[json.dumps(['dev', 'test'])]) # 👉️ tasks

The json.dumps method converts a Python object to a JSON formatted string. This works because strings are immutable and hashable.

Conversely, the json.loads method parses a JSON string into a native Python object, e.g. my_list = json.loads(my_json_str).

Most of the immutable built-in objects in Python are hashable, whereas mutable objects are unhashable.

If an object is hashable, then it can be used as a key in a dictionary and as an element in a set, because these data structures use the hash value internally

Hashable objects include - str, int, bool, tuple, frozenset.

Unhashable objects include - list, dict, set.

Note that tuples and frozensets are only hashable if their elements are hashable.

You can check if an object is hashable by passing it to the built-in hash() function.
print(hash('hello')) # 👉️ -1210368392134373610 # ⛔️ TypeError: unhashable type: 'list' print(hash(['a', 'b']))

The hash function returns the hash value of the passed in object (if it has one).

Hash values are integers and are used to compare dictionary keys during a dictionary lookup.

Hashable objects have a hash value which never changes during their lifetime. This is why most immutable objects are hashable, whereas mutable ones are unhashable.

Objects like lists are mutable because the contents of a list can be changed.
my_list = ['a', 'b', 'c'] my_list[0] = 'z' print(my_list) # ['z', 'b', 'c']

On the other hand, tuples that contain primitive values are immutable (and hashable).
my_tuple = ('a', 'b', 'c') # ⛔️ TypeError: 'tuple' object does not support item assignment my_tuple[0] = 'z'

Dictionaries are indexed by keys and the keys in a dictionary can be any immutable type, e.g. strings or numbers.

Tuples can only be used as keys in a dictionary if they contain strings, numbers or tuples.

If a tuple contains mutable objects such as lists, it cannot be used as a key in a dictionary or an element in a set.

If you aren't sure what type of object a variable stores, use the type() class.
my_list = ['a', 'b', 'c'] print(type(my_list)) # 👉️ <class 'list'> print(isinstance(my_list, list)) # 👉️ True my_tuple = ('a', 'b', 'c') print(type(my_tuple)) # 👉️ <class 'tuple'> print(isinstance(my_tuple, tuple)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

Use the search field on my Home Page to filter through my more than 1,000 articles.