Reverse dictionnary

Michael Hudson mwh at python.net
Fri Nov 15 06:30:14 EST 2002


Julien Barbot <barbot at shfj.cea.fr> writes:

> Does "reverse dictionnary" exists in python ?

No.  (At first I thought you meant reverse order, which makes no sense
as dictionaries have no order...)

> I would like to get a value with the key, and to get the key with the value
> For example:
> d1 = {1:"str1", 2:"str2" }
> 
> d1.getvalue(1) give me "str1"
> and 
> g1.getkey("str1") give me 1
> 
> With the result given as fast as normal dictionnaries.

No, and this can't really be done as values of a dictionary aren't
required to be hashable.  Or unique in the dict, for that matter.

> If this does not exists, I will simply do two dictionnaries, one with
> the integer as keys and the strings as values and the other one with
> the strings as keys and the intergers as values.

This is the best approach, I think.

Cheers,
M.

-- 
 Two decades later, well-known hacker Henry Spencer described the 
 Perl scripting language as a "Swiss-Army chainsaw", intending to 
 convey his evaluation of the language as exceedingly powerful but 
 ugly and noisy and prone to belch noxious fumes.   -- the jargon file



More information about the Python-list mailing list