Dictionaries of Lists

Terry Hancock hancock at anansispaceworks.com
Tue Mar 8 00:48:24 EST 2005


On Monday 07 March 2005 11:09 pm, 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.
> 
> 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.

You should try these things before assuming they don't
work!

There is no problem at all having a dictionary of lists.
This

d_of_l = { 'a': [0,1,2], 'b':[3,4,5]}

is perfectly legal.  Only the *keys* of a dictionary must
be hashable, any objects can be *values*.

If you *do* want to make a list into a tuple, BTW (e.g
if you decide you do want to use them as keys), then you
just need this:

tuple(mylist)


Cheers,
Terry

-- 
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com




More information about the Python-list mailing list