Multikey dictionary

Peter Hansen peter at engcorp.com
Tue Jul 23 22:20:56 EDT 2002


henk_derudder at hotmail.com wrote:
> 
> Sory for reposting this, I forgot to mention a subject in my first
> post :-)
> 
> What datatypes can one use as key in a dictionary?
> 
> Can I use for example two longs as key (kind of a double key)?
> 
> If not, are there examples on how to achieve this?

To experiment with this kind of thing the interactive
prompt can be highly effective.  You'll learn much more than
if you just ask the group (not to say that you won't learn
a lot from asking, too). 

>>> d = {}
>>> d[5] = 5
>>> d[5L]  = 6
>>> d
{5: 6}
>>> d[5, 6] = 7
>>> d
{(5, 6): 7, 5: 6}
>>> d[5L, 6L] = 88
>>> d
{(5, 6): 88, 5: 6}

Hmmm... looks like using "longs" (if you mean Python longs) is
no different from using regular ints of the same value, and you
can use two longs but they are obviously treated as though you'd
explicitly used a tuple even though the parentheses were left 
out.

-Peter



More information about the Python-list mailing list