Ordered dictionaries?

David Brady daves_spam_dodging_account at yahoo.com
Thu Dec 13 16:45:15 EST 2001


Hello,

One last Perl module that I need to replace to
complete my Python toolkit is a custom module that let
me create a dictionary whose keys were settable and
retrievable in a particular order.  Essentially, this
let us have a list whose ordered elements were also
accessible by name.

Has anyone done this?  If not, what class operators
would I need to override to create my own class that
does this?

Example of how it would work, assuming OrderedDict was
a working class of what I wanted:

>>> od = OrderedDict()
>>> od['Alice'] = 'Anderson'
>>> od['Bob'] = 'Bradley'
>>> od.append('Christiansen')
>>> od.['Dave'] = 'Dobson'
>>> od.keys()
['Alice', 'Bob', 2, 'Dave']
>>> od.values()
['Anderson', 'Bradley', 'Christiansen', 'Dobson']
>>> od[3]
'Dobson'
>>> od[7] = 'Johnson'
# Notice assignment does not trigger IndexError
>>> od.values()
['Anderson', 'Bradley', 'Christiansen', 'Dobson']
>>> len(od)
8
>>> for i in range(len(od)): print od[i]
Anderson
Bradley
Christiansen
Dobson
None
None
None
Johnson

...etc.  Notice that this is making the python list
act more like a Perl list, which may not be desirable.
 Also, I don't know if the None object "exists" in
terms of taking up space in the list, my thinking is
that it shouldn't; that trying to access an undefined
element should create the None object and return it
rather than padding the list with statically-created
None objects.

Anyway, if anyone has already done this, I'd love to
see it; if not, some tips about where to start would
be wonderful.  Is there a canonical list of all the
__functions__ a Python object can have, when they are
called and why, and what they must do if implemented
by a user?

Thank you,

-dB

=====
David Brady
daves_spam_dodging_account at yahoo.com
I'm feeling very surreal today... or *AM* I?

__________________________________________________
Do You Yahoo!?
Check out Yahoo! Shopping and Yahoo! Auctions for all of
your unique holiday gifts! Buy at http://shopping.yahoo.com
or bid at http://auctions.yahoo.com




More information about the Python-list mailing list