Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26178 > unrolled thread

Python Error

Started bysubhabangalore@gmail.com
First post2012-07-29 02:27 -0700
Last post2012-07-30 01:12 +1000
Articles 13 — 10 participants

Back to article view | Back to comp.lang.python


Contents

  Python Error subhabangalore@gmail.com - 2012-07-29 02:27 -0700
    Re: Python Error Peter Otten <__peter__@web.de> - 2012-07-29 13:08 +0200
    Re: Python Error subhabangalore@gmail.com - 2012-07-29 05:30 -0700
      Re: Python Error Thomas Jollans <t@jollybox.de> - 2012-07-29 14:45 +0200
      Re: Python Error Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-07-29 14:53 +0100
      Re: Python Error Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-29 14:01 +0000
      Re: Python Error Emile van Sebille <emile@fenx.com> - 2012-07-29 07:42 -0700
    Re: Python Error Roy Smith <roy@panix.com> - 2012-07-29 10:23 -0400
      Re: Python Error subhabangalore@gmail.com - 2012-07-29 07:41 -0700
    Re: Python Error Jürgen A. Erhard <jae@jaerhard.com> - 2012-07-29 15:57 +0200
      Re: Python Error Duncan Booth <duncan.booth@invalid.invalid> - 2012-07-30 11:15 +0000
    Re: Python Error subhabangalore@gmail.com - 2012-07-29 07:36 -0700
      Re: Python Error Chris Angelico <rosuav@gmail.com> - 2012-07-30 01:12 +1000

#26178 — Python Error

Fromsubhabangalore@gmail.com
Date2012-07-29 02:27 -0700
SubjectPython Error
Message-ID<81818a9c-60d3-48da-9345-0c0dfd5b25e7@googlegroups.com>
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. 

[toc] | [next] | [standalone]


#26179

FromPeter Otten <__peter__@web.de>
Date2012-07-29 13:08 +0200
Message-ID<mailman.2680.1343560135.4697.python-list@python.org>
In reply to#26178
subhabangalore@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'                                                                                                                                                                                

[toc] | [prev] | [next] | [standalone]


#26181

Fromsubhabangalore@gmail.com
Date2012-07-29 05:30 -0700
Message-ID<86285e84-bc6e-4527-9af5-c29f8c9716c0@googlegroups.com>
In reply to#26178
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. 
> 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.

[toc] | [prev] | [next] | [standalone]


#26182

FromThomas Jollans <t@jollybox.de>
Date2012-07-29 14:45 +0200
Message-ID<mailman.2682.1343566298.4697.python-list@python.org>
In reply to#26181
On 07/29/2012 02:30 PM, subhabangalore@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?

[toc] | [prev] | [next] | [standalone]


#26184

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-07-29 14:53 +0100
Message-ID<mailman.2684.1343569955.4697.python-list@python.org>
In reply to#26181
On 29/07/2012 13:30, subhabangalore@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.

[toc] | [prev] | [next] | [standalone]


#26185

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-07-29 14:01 +0000
Message-ID<5015422c$0$29978$c3e8da3$5496439d@news.astraweb.com>
In reply to#26181
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

[toc] | [prev] | [next] | [standalone]


#26193

FromEmile van Sebille <emile@fenx.com>
Date2012-07-29 07:42 -0700
Message-ID<mailman.2690.1343572962.4697.python-list@python.org>
In reply to#26181
On 7/29/2012 5:30 AM subhabangalore@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

[toc] | [prev] | [next] | [standalone]


#26189

FromRoy Smith <roy@panix.com>
Date2012-07-29 10:23 -0400
Message-ID<roy-91C870.10235829072012@news.panix.com>
In reply to#26178
In article <81818a9c-60d3-48da-9345-0c0dfd5b25e7@googlegroups.com>,
 subhabangalore@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?

[toc] | [prev] | [next] | [standalone]


#26192

Fromsubhabangalore@gmail.com
Date2012-07-29 07:41 -0700
Message-ID<e8403eed-7468-4442-8957-2f1d5c006002@googlegroups.com>
In reply to#26189
On Sunday, July 29, 2012 7:53:59 PM UTC+5:30, Roy Smith wrote:
> In article <81818a9c-60d3-48da-9345-0c0dfd5b25e7@googlegroups.com>,
> 
>  subhabangalore@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?

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.  

[toc] | [prev] | [next] | [standalone]


#26191

FromJürgen A. Erhard <jae@jaerhard.com>
Date2012-07-29 15:57 +0200
Message-ID<mailman.2689.1343572776.4697.python-list@python.org>
In reply to#26178
On Sun, Jul 29, 2012 at 01:08:57PM +0200, Peter Otten wrote:
> subhabangalore@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

[toc] | [prev] | [next] | [standalone]


#26245

FromDuncan Booth <duncan.booth@invalid.invalid>
Date2012-07-30 11:15 +0000
Message-ID<XnsA0A07C8AD2FD0duncanbooth@127.0.0.1>
In reply to#26191
Jürgen A. Erhard <jae@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

[toc] | [prev] | [next] | [standalone]


#26194

Fromsubhabangalore@gmail.com
Date2012-07-29 07:36 -0700
Message-ID<35b4e1bf-0f99-4c9d-bd40-8f44ba100510@googlegroups.com>
In reply to#26178
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)
> 
> 
> 
> 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 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. 

[toc] | [prev] | [next] | [standalone]


#26195

FromChris Angelico <rosuav@gmail.com>
Date2012-07-30 01:12 +1000
Message-ID<mailman.2691.1343574742.4697.python-list@python.org>
In reply to#26194
On Mon, Jul 30, 2012 at 12:36 AM,  <subhabangalore@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web