dictionary with complex keys

Frank Niessink frankn=nuws at cs.vu.nl
Tue Jun 6 16:00:07 EDT 2000


Stefan Seefeld <seefelds at magellan.umontreal.ca> wrote:
> I'm trying to create a dictionary with the keys being lists.
> How can I do that ? The docs say that only simple types are
> possible as keys. Is there a way to achieve what I want ? May
> be some special operator I need to define...

Without knowing more about your lists, this is difficult to
answer. A simple solution would be to use tuple(mylist) as
key, e.g.:
>>> mylist = [1,2,3]
>>> dict = {}
>>> dict[tuple(mylist)] = 1
>>> dict
{(1, 2, 3): 1}

Another solution could be to subclass UserList and add your
own __hash__() function.

Cheers, Frank

-- 
"Ford," he said, "you're turning into a penguin. Stop it."
	-- Douglas Adams, 'The Hitch Hiker's Guide to the Galaxy'



More information about the Python-list mailing list