Dictionaries of Lists

G

gf gf

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.

Thanks!

PS If possible please cc me on all responses.





__________________________________
Celebrate Yahoo!'s 10th Birthday!
Yahoo! Netrospective: 100 Moments of the Web
http://birthday.yahoo.com/netrospective/
 
E

Erik Max Francis

gf said:
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.
 
R

Robert Kern

gf said:
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
(e-mail address removed)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top