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


Groups > comp.lang.python > #95762

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

Path csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!bcyclone02.am1.xlned.com!bcyclone02.am1.xlned.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'value,': 0.03; 'none,': 0.05; "'')": 0.07; 'column': 0.07; "subject:' ": 0.07; "'0',": 0.09; 'csv': 0.09; 'dict': 0.09; 'iterate': 0.09; 'loop.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'type:': 0.09; 'python': 0.10; 'index': 0.13; 'headers': 0.15; '12:00:00': 0.16; 'iterates': 0.16; 'keys.': 0.16; 'list):': 0.16; 'matters,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'row': 0.16; 'sequence,': 0.16; 'so!': 0.16; 'subject:type': 0.16; 'subject:when': 0.16; 'unhashable': 0.16; 'instance,': 0.18; 'meant': 0.22; 'saying': 0.22; 'assumes': 0.22; 'item.': 0.22; 'keys': 0.22; 'occurs': 0.22; '(where': 0.23; 'header:User- Agent:1': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'order.': 0.27; 'format,': 0.27; 'sequence': 0.27; 'idea': 0.28; "skip:' 10": 0.28; 'values': 0.28; 'attempting': 0.29; 'dictionary': 0.29; 'hash': 0.29; 'loop,': 0.29; 'skip:[ 10': 0.31; 'fixed': 0.31; 'statement': 0.32; 'getting': 0.33; '"")': 0.33; 'structure': 0.34; 'list': 0.34; 'attempt': 0.35; 'i.e.': 0.35; 'instance': 0.35; 'mapping': 0.35; 'robert': 0.35; "isn't": 0.35; 'item': 0.35; 'but': 0.36; 'list,': 0.36; 'there': 0.36; 'skip:{ 10': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'responsible': 0.37; 'received:org': 0.37; 'itself': 0.38; 'thank': 0.38; 'end': 0.39; 'data': 0.39; 'to:addr:python.org': 0.40; 'information': 0.63; 'skip:w 30': 0.64; 'goal': 0.64; 'believe': 0.66; 'skip:\xe2 10': 0.70; '8bit%:43': 0.72; '8bit%:46': 0.76; 'integrity': 0.76; '_o__)': 0.84; 'dict.': 0.84; 'front.': 0.84; 'received:125': 0.84; 'notebook': 0.96
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought
Date Sun, 30 Aug 2015 13:04:24 +1000
References <cbcf05be-53d0-4b8b-b72b-1898cd4ad245@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock sha1:CFeVMtxOHCXqoacpUu56T7wsosk=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.131.1440903881.11709.python-list@python.org> (permalink)
Lines 80
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1440903881 news.xs4all.nl 23792 [2001:888:2000:d::a6]:48700
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 6592
X-Received-Body-CRC 2802708726
Xref csiph.com comp.lang.python:95762

Show key headers only | View raw


kbtyo <ahlusar.ahluwalia@gmail.com> writes:

> I am using Jupyter Notebook and Python 3.4.

Thank you for saying so! It is not always required, but when it matters,
this information is important to state up front.

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

So that assumes that *every* item will be a mapping with all the same
keys. CSV is limited to a sequence of “records” which all have the same
fields in the same order.

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

[…]
>         for row in results:
>             data = [row.get(index, '') for index in results]

The ‘for’ statement iterates over ‘results’, getting an item each time.
The name ‘row’ is bound to each item in turn.

Then, each time through the ‘for’ loop, you iterate *again* over
‘results’. The name ‘index’ is bound to each item.

You then attempt to use the dict (each item from ‘results’ is itself a
dict) as a key into that same dict. A dict is not a valid key; it is not
a “hashable type” i.e. a type with a fixed value, that can produce a
hash of the value).

So you're getting dicts and attempting to use those dicts as keys into
dicts. That will give the error “TypeError: unhashable type: 'dict'”.

I think what you want is not items from the original sequence, but the
keys from the mapping::

    for input_record in results:
        output_record = [input_record.get(key, "") for key in input_record]

But you're then throwing away the constructed list, since you do nothing
with it before the end of the loop.

>         writer.writerow(data)

This statement occurs only *after* all the items from ‘results’ have
been iterated. You will only have the most recent constructed row.

Perhaps you want::

    for input_record in results:
        output_record = [input_record.get(key, "") for key in input_record]
        writer.writerow(output_record)

-- 
 \       “An idea isn't responsible for the people who believe in it.” |
  `\                                      —Donald Robert Perry Marquis |
_o__)                                                                  |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-29 19:05 -0700
  Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-30 03:47 +0100
    Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-30 09:35 -0700
      Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought Laura Creighton <lac@openend.se> - 2015-08-30 19:24 +0200
        Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-30 10:34 -0700
          Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought Laura Creighton <lac@openend.se> - 2015-08-30 20:20 +0200
            Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-30 14:47 -0700
  Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought MRAB <python@mrabarnett.plus.com> - 2015-08-30 03:48 +0100
    Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-30 09:31 -0700
      Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought MRAB <python@mrabarnett.plus.com> - 2015-08-30 18:15 +0100
        Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-30 10:21 -0700
  Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought Ben Finney <ben+python@benfinney.id.au> - 2015-08-30 13:04 +1000
    Re: TypeError: unhashable type: 'dict' when attempting to hash list - advice sought kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-08-30 10:02 -0700

csiph-web