Dictionaries of Lists

Robert Kern rkern at ucsd.edu
Tue Mar 8 06:32:27 CET 2005


gf gf wrote:
> I'd like to associate certain lists with keywords, and
> retrieve them.  But this is not possible as lists are
> not hashable.

Do you want

   mydict[mylist] = mykey

or

   mydict[mykey] = mylist

?

The former is not possible with lists for the reason you noted. The 
latter, however, works just fine. It is only the key that needs to be 
hashable. The value can be any object.

> What is the best workaround?  I don't mind making my
> lists immutable.  Is there a way to tupelize them?

   tuple(mylist)

> I tried mydict[mykey]=([a for a in list]) but it
> didn't seem to work.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the Python-list mailing list