reverse dict lookup & Relation class

MRAB google at
Thu Jan 15 02:54:49 CET 2009

Aaron Brady wrote:
> Hi, this is a continuation of something that comes up now and again 
> about reverse lookups on dictionaries, as well as a follow-up to my 
> pursuit of a Relation class from earlier.
> For a reverse lookup, you just need two lookups.
> name= {}
> phone= {}
> name[ '555-963' ]= 'Joan'
> phone[ 'Joan' ]= '555-963'
> Though maybe the keys in 'name' should be names, instead of the
> values in it.  The variable name doesn't clarify that.  (What is more
> natural to you in reading code?  Is one obviously wrong?)
> phone[ '555-963' ]= 'Joan'
> name[ 'Joan' ]= '555-963'
Consider "str(x)". It isn't saying "x is a string", it's saying "x as a
string". Similarly, "ord(x)" says "the ordinal value of x". So,
"phone[x]" means "the phone number of x".

> What's the best way to construct this class?  Or, do you have an 
> argument that it could not be simpler than using a relational db? 
> Brainstorming, not flamestorming.
You could imagine an object where you provide a field name and a value
and it returns all those entries where that field contains that value.
That, basically, is a database.

More information about the Python-list mailing list