Python Error

Peter Otten __peter__ at web.de
Sun Jul 29 13:08:57 CEST 2012


subhabangalore at gmail.com wrote:

> Dear Group,
> 
> I was trying to convert the list to a set, with the following code:
> 
> set1=set(list1)
> 
> the code was running fine, but all on a sudden started to give the
> following error,
> 
> set1=set(list1)
> TypeError: unhashable type: 'list'
> 
> please let me know how may I resolve.
> 
> And sometimes some good running program gives error all on a sudden with
> no parameter changed, how may I debug it?

Add a print statement before the offending line:

print list1
set1 = set(list1)

You will see that list1 contains another list, e. g. this works...

>>> list1 = ["alpha", "beta"]                                                                                                                                                                                     
>>> set(list1)                                                                                                                                                                                                    
set(['alpha', 'beta'])                                                                                                                                                                                            

...while this doesn't:

>>> list1 = ["alpha", ["beta"]]                                                                                                                                                                                   
>>> set(list1)                                                                                                                                                                                   
Traceback (most recent call last):                                                                                                                                                                                
  File "<stdin>", line 1, in <module>                                                                                                                                                                             
TypeError: unhashable type: 'list'                                                                                                                                                                                




More information about the Python-list mailing list