Python Error

S

subhabangalore

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?

Thanking You in Advance,

Regards,
Subhabrata Banerjee.
 
P

Peter Otten

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

subhabangalore

Dear Group,



I was trying to convert the list to a set, with the following code:



set1=set(list1)
Dear Peter,
Thanks for the answer. But my list does not contain another list that is the issue. Intriguing. Thinking what to do.
Regards,
Subhabrata.
 
T

Thomas Jollans

Thanks for the answer. But my list does not contain another list that is the issue. Intriguing. Thinking what to do.

What does your list contain? Can you reproduce the issue in a few
self-contained lines of code that you can show us, that we can test
ourselves?
 
M

Mark Lawrence

Dear Peter,
Thanks for the answer. But my list does not contain another list that is the issue. Intriguing. Thinking what to do.
Regards,
Subhabrata.

Can you loop round the list and print each entry and its type, that
should give you some clues?
 
J

Jürgen A. Erhard

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

Peter's right, but instead of a print before the line, put a
try/except around it, like

try:
set1 = set(list1)
except TypeError:
print list1
raise

This way, only the *actual* error triggers any output. With a general
print before, you can get a lot of unnecessary output.

Grits, J
 
S

Steven D'Aprano

Dear Peter,
Thanks for the answer. But my list does not contain another list that is
the issue. Intriguing.

That is not what the error message says. You said that this line of code:

set1=set(list1)

gives this error:

TypeError: unhashable type: 'list'


Almost certainly, either you are mistaken about the line of code which
gives the error, or you are mistaken about the error, or you are mistaken
that your list does not contain any lists.

Thinking what to do.

Exactly what Peter suggested: print the list before you try to convert it
to a set, and see what it actually contains.

It will also help you to read this page and try to follow its advice:

http://sscce.org/
 
R

Roy Smith

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?
 
S

subhabangalore

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?



Thanking You in Advance,



Regards,

Subhabrata Banerjee.

Dear Group,

Thank you for your kind time and reply. True as Steven pointed the error should be specific. I tested. Put comment mark before the set assignment, printed the list as Peter suggested, taken the print of the list, but no it isnot my problem, as you suggested what is contained in the list, I am taking out the values and then assigning blank list and appending the processed values in the list.
If this kind of problems happen, --rare but in my 6 yrs Python experience happened sometimes--then I take a new window, rewrite or copy the earlier code module by module, give a new method name--believe it or not--- works.

Regards,
Subhabrata.
 
S

subhabangalore

(e-mail address removed) wrote:






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







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?

Hi Roy,
Sorry I overlooked your answer. It fails generally on Sunday. True. How you got it? I recently downloaded Python2.7 64 bit -while I am working on Python3.2.1 64 bit Windows 7 SP1.
Regards,
Subhabrata Banerjee.
 
E

Emile van Sebille

Dear Group,
I was trying to convert the list to a set, with the following code:
set1=set(list1)
Thanks for the answer. But my list does not contain another list that is the issue. Intriguing. Thinking what to do.[/QUOTE]

Now you need to identify the type of the object that is causing python
to misreport the unhashable type causing the error as the error you're
getting says list and you say there isn't one. So, now we have a python
bug.
set ([1,2,3]) set([1, 2, 3])
set ([1,2,[]])
Traceback (most recent call last):
File said:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'


the code was running fine, but all on a sudden started to give the following error,



set1=set(list1)

TypeError: unhashable type: 'list'


Try adding the following:

for ii in list1:
try:
set([ii])
except:
print "this causes an error type (val): %s (%s)" (type(ii),ii)


Either it's a python bug or there really is a list in there.

Emile
 
C

Chris Angelico

If this kind of problems happen, --rare but in my 6 yrs Python experience happened sometimes--then I take a new window, rewrite or copy the earlier code module by module, give a new method name--believe it or not--- works.

If that solves your problem, it may be that you inadvertently shadowed
a built-in - maybe you assigned to "set" or "list" or something.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top