Dictionaries of Lists

Erik Max Francis max at alcyone.com
Tue Mar 8 00:26:40 EST 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.

You can convert them to tuples with the `tuple' function:

	aDictionary[tuple(aList)] = aKeyword

 > What is the best workaround?  I don't mind making my
 > lists immutable.  Is there a way to tupelize them?
 > I tried mydict[mykey]=([a for a in list]) but it
 > didn't seem to work.

This code sample seems to suggest you're trying to do something else, 
however.  If `mykey' is a string, then what you wrote is completely 
valid regardless of whether you converted it to a list or not.

If that was just a thinko while you were asking the question, then the 
reason ([...]) didn't work is that it's just syntactically wrapping a 
list comprehension in parentheses, rather than turning them into a tuple.

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   Dead men have no victory.
   -- Euripides



More information about the Python-list mailing list