TypeError: unhashable type: ‘list’

In this post, we will see about TypeError: unhashable type: 'list'.

You will get this error when you are trying to put list as key in dictionary or set because list is unhashable object.TypeError: unhashable type is generally raised when you try to hash object which is unhashable.

Let’s see this with help of example:

Output:

You can resolve this issue by casting list to tuple. Since tuple is immutable object, it can be used as key in dictionary.

Output:

{1: ‘one’, (2, 10): ‘two’}

As you can see, once we use tuple() function, program worked fine.
Let’s see another example with set.

Output:

You can resolve this issue by casting list to tuple. Since tuple is immutable object, it can be used as key in dictionary.

Output:

{(1, 2), (3, 4)}

As you can see, once we use tuple() function, program worked fine.

What are Hashable objects?

Hashing is a method of encoding data into fixed-size int value. It is generally used to design high performing data structure.

Hashable object in Python

intfloatdecimalboolstringtuplecomplexrangefrozensetbytes

Non Hashable object in Python

listsetdictbytearraycustom classes

That’s all about TypeError: unhashable type: ‘list’.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.