TypeError: unhashable type

Thomas Wouters thomas at xs4all.net
Tue Jun 6 12:15:46 EDT 2000


On Tue, Jun 06, 2000 at 10:38:44AM -0500, sh wrote:

> This works without a glitch in the command line interpreter:
>     segments = {}        # dictionary type
>     segment = []
>     # populate segment
>     segments[segment[0]] = segment[len(segment)-1]
> 
> BUT, when I run it from the DOS prompt: "python segments.py", I got error
> message:
>     TypeError: unhashable type.

> Any explanation is appreciated.  Thanks.

You ommited some code, namely the code that fills the list 'segment'. You
are probably filling it with unhashable objects (mutable objects, in general.)
Dictionary keys have to be hashable, which means they have to be immutable
(or at least always return the same hash value.)

If you populate 'segment' with lists, try populating it with tuples. Tuples
are immutable, and can be used as dictionary keys.

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list