TypeError: unhashable type: 'dict' when attempting to hash list - advice sought

kbtyo ahlusar.ahluwalia at gmail.com
Sun Aug 30 12:35:20 EDT 2015


On Saturday, August 29, 2015 at 10:50:19 PM UTC-4, Mark Lawrence wrote:
> On 30/08/2015 03:05, kbtyo wrote:
> > I am using Jupyter Notebook and Python 3.4. I have a data structure in the format, (type list):
> >
> > [{'AccountNumber': N,
> > 'Amount': '0',
> >   'Answer': '12:00:00 PM',
> >    'ID': None,
> >    'Type': 'WriteLetters',
> >    'Amount': '10',
> >    {'AccountNumber': Y,
> >        'Amount': '0',
> >        'Answer': ' 12:00:00 PM',
> >         'ID': None,
> >        'Type': 'Transfer',
> >        'Amount': '2'}]
> >
> > The end goal is to write this out to CSV.
> >
> > For the above example the output would look like:
> >
> > AccountNumber, Amount, Answer, ID, Type, Amount
> > N,0,12:00:00 PM,None,WriteLetters,10
> > Y,2,12:00:00 PM,None,Transfer,2
> >
> > Below is the function that I am using to write out this data structure. Please excuse any indentation formatting issues. The data structure is returned through the function "construct_results(get_just_xml_data)".
> >
> > The data that is returned is in the format as above. "construct_headers(get_just_xml_data)" returns a list of headers. Writing out the row for "headers_list" works.
> >
> > The list comprehension "data" is to maintain the integrity of the column headers and the values for each new instance of the data structure (where the keys in the dictionary are the headers and values - row instances). The keys in this specific data structure are meant to check if there is a value instance, and if there is not - place an ''.
> >
> > def write_to_csv(results, headers):
> >
> >      headers = construct_headers(get_just_xml_data)
> >      results = construct_results(get_just_xml_data)
> >      headers_list = list(headers)
> >
> >      with open('real_csv_output.csv', 'wt') as f:
> >          writer = csv.writer(f)
> >          writer.writerow(headers_list)
> >          for row in results:
> >              data = [row.get(index, '') for index in results]
> >          writer.writerow(data)
> >
> >
> >
> > However, when I run this, I receive this error:
> >
> > ---------------------------------------------------------------------------
> > TypeError                                 Traceback (most recent call last)
> > <ipython-input-747-7746797fc9a5> in <module>()
> > ----> 1 write_to_csv(results, headers)
> >
> > <ipython-input-746-c822437eeaf0> in write_to_csv(results, headers)
> >        9         writer.writerow(headers_list)
> >       10         for item in results:
> > ---> 11             data = [item.get(index, '') for index in results]
> >       12         writer.writerow(data)
> >
> > <ipython-input-746-c822437eeaf0> in <listcomp>(.0)
> >        9         writer.writerow(headers_list)
> >       10         for item in results:
> > ---> 11             data = [item.get(index, '') for index in results]
> >       12         writer.writerow(data)
> >
> > TypeError: unhashable type: 'dict'
> >
> >
> > I have done some research, namely, the following:
> >
> > https://mail.python.org/pipermail//tutor/2011-November/086761.html
> >
> > http://stackoverflow.com/questions/27435798/unhashable-type-dict-type-error
> >
> > http://stackoverflow.com/questions/1957396/why-dict-objects-are-unhashable-in-python
> >
> > However, I am still perplexed by this error. Any feedback is welcomed. Thank you.
> >
> 
> I haven't looked too deeply into the problem as it's 03:45 and I'm just 
> heading off to bed, but I suspect you'd make your life easier by using 
> https://docs.python.org/3/library/csv.html#csv.DictWriter
> 
> -- 
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
> 
> Mark Lawrence



On Saturday, August 29, 2015 at 10:50:19 PM UTC-4, Mark Lawrence wrote:
> On 30/08/2015 03:05, kbtyo wrote:
> > I am using Jupyter Notebook and Python 3.4. I have a data structure in the format, (type list):
> >
> > [{'AccountNumber': N,
> > 'Amount': '0',
> >   'Answer': '12:00:00 PM',
> >    'ID': None,
> >    'Type': 'WriteLetters',
> >    'Amount': '10',
> >    {'AccountNumber': Y,
> >        'Amount': '0',
> >        'Answer': ' 12:00:00 PM',
> >         'ID': None,
> >        'Type': 'Transfer',
> >        'Amount': '2'}]
> >
> > The end goal is to write this out to CSV.
> >
> > For the above example the output would look like:
> >
> > AccountNumber, Amount, Answer, ID, Type, Amount
> > N,0,12:00:00 PM,None,WriteLetters,10
> > Y,2,12:00:00 PM,None,Transfer,2
> >
> > Below is the function that I am using to write out this data structure. Please excuse any indentation formatting issues. The data structure is returned through the function "construct_results(get_just_xml_data)".
> >
> > The data that is returned is in the format as above. "construct_headers(get_just_xml_data)" returns a list of headers. Writing out the row for "headers_list" works.
> >
> > The list comprehension "data" is to maintain the integrity of the column headers and the values for each new instance of the data structure (where the keys in the dictionary are the headers and values - row instances). The keys in this specific data structure are meant to check if there is a value instance, and if there is not - place an ''.
> >
> > def write_to_csv(results, headers):
> >
> >      headers = construct_headers(get_just_xml_data)
> >      results = construct_results(get_just_xml_data)
> >      headers_list = list(headers)
> >
> >      with open('real_csv_output.csv', 'wt') as f:
> >          writer = csv.writer(f)
> >          writer.writerow(headers_list)
> >          for row in results:
> >              data = [row.get(index, '') for index in results]
> >          writer.writerow(data)
> >
> >
> >
> > However, when I run this, I receive this error:
> >
> > ---------------------------------------------------------------------------
> > TypeError                                 Traceback (most recent call last)
> > <ipython-input-747-7746797fc9a5> in <module>()
> > ----> 1 write_to_csv(results, headers)
> >
> > <ipython-input-746-c822437eeaf0> in write_to_csv(results, headers)
> >        9         writer.writerow(headers_list)
> >       10         for item in results:
> > ---> 11             data = [item.get(index, '') for index in results]
> >       12         writer.writerow(data)
> >
> > <ipython-input-746-c822437eeaf0> in <listcomp>(.0)
> >        9         writer.writerow(headers_list)
> >       10         for item in results:
> > ---> 11             data = [item.get(index, '') for index in results]
> >       12         writer.writerow(data)
> >
> > TypeError: unhashable type: 'dict'
> >
> >
> > I have done some research, namely, the following:
> >
> > https://mail.python.org/pipermail//tutor/2011-November/086761.html
> >
> > http://stackoverflow.com/questions/27435798/unhashable-type-dict-type-error
> >
> > http://stackoverflow.com/questions/1957396/why-dict-objects-are-unhashable-in-python
> >
> > However, I am still perplexed by this error. Any feedback is welcomed. Thank you.
> >
> 
> I haven't looked too deeply into the problem as it's 03:45 and I'm just 
> heading off to bed, but I suspect you'd make your life easier by using 
> https://docs.python.org/3/library/csv.html#csv.DictWriter

I have already looked at this Mark. The problem is that my headers were a set (which were then converted into a list object for the csv writer module to use). I had to resort to a set in order to collect new headers as they were generated on the fly with my XML parser. This is not relevant to this question, but I thought I would provide some context. It would be easier with if each column header mapped perfectly with each value. Turns out they don't. Also, I would not recommend sleeping so late - even on weekends. That can really disrupt your circadian rhythm :)
> 
> -- 
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
> 
> Mark Lawrence



More information about the Python-list mailing list