Multikey dictionary
Bengt Richter
bokr at oz.net
Wed Jul 24 20:23:12 EDT 2002
On Tue, 23 Jul 2002 13:26:57 GMT, henk_derudder at hotmail.com wrote:
>Sory for reposting this, I forgot to mention a subject in my first
>post :-)
>
>Hi,
>
>What datatypes can one use as key in a dictionary?
Pretty much anything non-mutable. It'll complain if you
give it something unhashable.
>
>Can I use for example two longs as key (kind of a double key)?
sure, just put them in a tuple and use that as key.
>>> d={}
>>> a=[1]
>>> b=[2]
>>> d[ (a,b) ] = 'zee'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list objects are unhashable
>>>
>>> d[ (1,2) ] = 'one comma two'
>>> d[ (3,4) ] = 'three comma four'
>>> d[(1,2)]
'one comma two'
>>> a=(1,2)
>>> d[a]
'one comma two'
>>> a=(3,4)
>>> d[a]
'three comma four'
>
>If not, are there examples on how to achieve this?
>
Regards,
Bengt Richter
More information about the Python-list
mailing list