[Tutor] how to understand unhashable type: 'list'

Walter Prins wprins at gmail.com
Thu Nov 17 15:16:03 CET 2011


Hi Lina

On 17 November 2011 14:04, lina <lina.lastname at gmail.com> wrote:

> >> Traceback (most recent call last):
> >>  File "<pyshell#292>", line 1, in <module>
> >>    weight[list1[0]]=1
> >> TypeError: unhashable type: 'list'
> >
> > You are trying to store a list as a key inside a dict. This cannot be
> done
> > because lists (like all mutable types) can't be hashed.
>
> I checked online dictionary, still confused about hashed. is it equal
> to mix together or mess together?
>

No.... you need to think programming/computer science where hash/hashing
has a different meaning.  See wikipedia:
http://en.wikipedia.org/wiki/Hash_function
http://en.wikipedia.org/wiki/Hash_table

Also see the description for "hashable" in the Python glossary:
http://docs.python.org/glossary.html#hashable

Basically hashing is a way to convert something (often a string) into an
identifying number (not neccesarily but usually unique) in order to use
this number as a token for the original thing.  Aside, you can infer that
that because the "dict" code complains about hashing implies that dicts
themselves are essentially implemented as hash tables... ;)

HTH,

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111117/d87b8000/attachment.html>


More information about the Tutor mailing list