Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Python Error

51 views
Skip to first unread message

subhaba...@gmail.com

unread,
Jul 29, 2012, 5:27:18 AM7/29/12
to
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.

Peter Otten

unread,
Jul 29, 2012, 7:08:57 AM7/29/12
to pytho...@python.org
subhaba...@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'

subhaba...@gmail.com

unread,
Jul 29, 2012, 8:30:15 AM7/29/12
to
On Sunday, July 29, 2012 2:57:18 PM UTC+5:30, (unknown) wrote:
> 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.

Thomas Jollans

unread,
Jul 29, 2012, 8:45:21 AM7/29/12
to pytho...@python.org
On 07/29/2012 02:30 PM, subhaba...@gmail.com wrote:
> 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?

Mark Lawrence

unread,
Jul 29, 2012, 9:53:44 AM7/29/12
to pytho...@python.org
On 29/07/2012 13:30, subhaba...@gmail.com wrote:
> On Sunday, July 29, 2012 2:57:18 PM UTC+5:30, (unknown) wrote:
>> 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.

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

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


--
Cheers.

Mark Lawrence.

Steven D'Aprano

unread,
Jul 29, 2012, 10:01:17 AM7/29/12
to
On Sun, 29 Jul 2012 05:30:15 -0700, subhabangalore wrote:

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



--
Steven

Roy Smith

unread,
Jul 29, 2012, 10:23:59 AM7/29/12
to
In article <81818a9c-60d3-48da...@googlegroups.com>,
subhaba...@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?

Jürgen A.

unread,
Jul 29, 2012, 9:57:48 AM7/29/12
to pytho...@python.org
On Sun, Jul 29, 2012 at 01:08:57PM +0200, Peter Otten wrote:
> subhaba...@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'
> >
>
> 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

subhaba...@gmail.com

unread,
Jul 29, 2012, 10:41:47 AM7/29/12
to
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.

Emile van Sebille

unread,
Jul 29, 2012, 10:42:24 AM7/29/12
to pytho...@python.org
On 7/29/2012 5:30 AM subhaba...@gmail.com said...
> On Sunday, July 29, 2012 2:57:18 PM UTC+5:30, (unknown) wrote:
>> 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.

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 "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> set ([1,2,{}])
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


subhaba...@gmail.com

unread,
Jul 29, 2012, 10:36:39 AM7/29/12
to
On Sunday, July 29, 2012 2:57:18 PM UTC+5:30, (unknown) wrote:
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 is not 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.

Chris Angelico

unread,
Jul 29, 2012, 11:12:19 AM7/29/12
to pytho...@python.org
On Mon, Jul 30, 2012 at 12:36 AM, <subhaba...@gmail.com> wrote:
> 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

Duncan Booth

unread,
Jul 30, 2012, 7:15:06 AM7/30/12
to
J�rgen A. Erhard <j...@jaerhard.com> wrote:

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

Or even better:

try:
set1 = set(list1)
except TypeError:
print list1
import pdb; pdb.set_trace()
raise

Then the error will print the value of list1 and drop you into the debugger
so you can examine what's going on in more detail.

--
Duncan Booth http://kupuguy.blogspot.com
0 new messages