Lists and Tuples and Much More

Hendrik van Rooyen mail at microcorp.co.za
Sat Apr 14 03:07:52 EDT 2007



 "7stud" <bb..9_05ss at yahoo.com> wrote:

> ..........  But using a tuple as a
> key in a dictionary is probably something you will never do.

Yikes!  I do this all the time...

Think of an address in any one town.
It has a Street, and a number 
(could be more complex, like a sub number for an apartment 
in a block, but lets ignore that for now)

so:
addy = ("First Avenue", 10)
Residents = {addy:"Frank Everyman",......}

To make a thing where you can look up who lives at a place...
(this is simplistic of course, for illustrative purposes - few people
live completely alone. - it should be at least a list, or possibly a dict 
by surname...)

I find that when I do this, I almost invariably also create the
inverse dict:

Addresses = {"Frank Everyman":addy,.....}

So that you can get at stuff from all directions.  If you keep it
up you have a home made relational database if you are not careful...

But if you limit it to one thing and its inverse, its quite useful, and it
would be nice to have one "doubledict" that can be accessed as speedily
from either end...

Sort of an internally linked list of mixed hashed immutables, where: 

doubledict["Frank Everyman"] yields addy, and
doubledict[addy] yields "Frank Everyman"

It would have direct applicability in things like the label table in
an assembler, for debugging and so on.

- Hendrik




More information about the Python-list mailing list