Vishnu M U Vishnu M U - 1 month ago 9
Python Question

Python list operation error : unhashable type 'list'

I have the below list.

A = [['a', 10], ['b', 50], ['d', 20],['b', 50]]


why can't I do :

B = set(A)


I'm getting this error:

TypeError: unhashable type: 'list'


I'm not using a dictionary as you can see. A is a list of lists.

Answer

A set requires its members to be hashable as well. You should use a list of tuples instead of a list of lists:

A = [('a', 10), ('b', 50), ('d', 20), ('b', 50)]

A set is basically a dictionary without values for the keys.

Comments