Vishnu M U Vishnu M U - 1 year ago 114
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download