Hashing arrays in Python Hashing arrays in Python arrays arrays

Hashing arrays in Python


Just try it:

>>> hash((1,2,3))2528502973977326415>>> hash([1,2,3])Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: unhashable type: 'list'>>> hash(frozenset((1,2,3)))-7699079583225461316>>> hash(set((1,2,3)))Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: unhashable type: 'set'

So you can get hash of tuple and frozenset since the are immutable, and you can't do it for list and set because they are mutable.


If you really need to use a list as a dictionary key, try converting it to a string first.
my_list = str(my_list)


Python doesn't allow you to use mutable data as keys in dictionaries, because changes after insertion would make the object un-findable. You can use tuples as keys.