Python Error

Roy Smith roy at panix.com
Sun Jul 29 10:23:59 EDT 2012


In article <81818a9c-60d3-48da-9345-0c0dfd5b25e7 at googlegroups.com>,
 subhabangalore at gmail.com wrote:

> 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'

First, make sure you understand what the error means.  All the elements 
of a set must be hashable.  Lists are not hashable because they are 
mutable.  So, what the error is telling you is that some element of 
list1 is itself a list, and therefore not hashable, and thus the set 
can't be created.

I would start by printing list1.  If the list is long (or contains 
deeply nested structures), just doing "print list1" may result in 
something that is difficult to read.  In that case, try using pprint 
(see the pprint module) to get a nicer display.

If it's still not obvious, pull out the bigger guns.  Try something like:

for item in list1:
   try:
      hash(item)
   except TypeError:
      print "This one can't be hashed: %s" % item

> And sometimes some good running program gives error all on a sudden with no 
> parameter changed

Well, *something* changed.  Assuming nothing truly bizarre like a stray 
Higgs Boson flipping a bit in your computer's memory, what you need to 
do is figure out what that is.  Did you change your code in any way 
(having everything under version control helps here)?  If not the code, 
then what changed about the input?

If you're sure that both the code and the input are unchanged, that 
leaves something in the environment.  Did your python interpreter get 
upgraded to a newer version?  Or your operating system?  PYTHONPATH?

Depending on what your program is doing, it could be something time 
based.  A different time zone, perhaps?  Did daylight savings time just 
go into or out of effect where you are?  Does it only fail on Sunday?



More information about the Python-list mailing list