an ordered dictionary

Jeff Pinyan jeffp at crusoe.net
Tue Mar 14 17:27:13 EST 2000


Ordinary (real) dictionaries aren't ordered.  Coming from a Perl
environment, I decided to emulate Tie::IxHash, a module that allows for
ordered (or specified order) hashes (same as dictionaries).

Now, I'm not sure if I should make the keys() OrderedDict method return
the internal list of keys, such that

  od.keys()[1] = 'FOO'

would change a key in the OrderedDict object od.  The internal
representation of an OrderedDict is

  self.KEYS = [ ... ]
  self.hash = { ... }

the hash attribute being a regular dict, and the KEYS attribute being the
list of keys in whatever order they are to be in.  I've been thinking of
changing it, so that values() could be messed with, too, but I don't think
that will be needed, since values can be accessed via index as well as
key.

If anyone's interested, the source is online at

  http://www.pobox.com/~japhy/tmp/python/OrderedDict.py

-- 
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve             japhy at pobox.com
http://www.pobox.com/~japhy/                  http://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/




More information about the Python-list mailing list