Multikey Dict?

David Rasmussen david.rasmussen at gmx.net
Sun Nov 13 01:03:05 CET 2005


If I have a collection of dicts like:

john = {'id': 1, 'name': "John Cleese", 'year': 1939}
graham = {'id': 2, 'name': "Graham Chapman", 'year': 1941}

I could store all of them in a list. But for easy lookup, I might store 
all these in a dict instead, like

people = {'1': john, '2': graham}

or maybe

people = {'John Cleese': john, 'Graham Chapman': graham}

or whatever key I might choose. Now, first of all, it seems a bit 
annoying that I have to keep that redundant data in the second dict that 
is already in the individual dicts within people. Secondly (and this is 
my question), it is annoying that I have to choose one of several 
unambiguous keys as a key.

I would like to be able to say:

people['1'].year

in some case and in other cases I want to say

people['John Cleese'].year

That is, sometimes I have the name at hand and would like to look up 
data based on that. Other times, I have the ID at hand and would like to 
look up data based on that instead.

Also, I would like if I didn't have to keep the key data both in the 
dict of dicts and in the dicts :)

If I could just say to Python: john and graham (and ...) are all a part 
of a "superdict" and either their id or their name can be used as keys.

Can I do that somehow?

/David



More information about the Python-list mailing list