Ordered dictionaries?

Martin Franklin martin.franklin at westgeo.com
Thu Dec 13 23:40:53 CET 2001

David Brady wrote:

> 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

One way to go would be:

from UserDict import UserDict

class OrderedDict(UserDict):
    def __init__(self):
    def keys(self):
        return keys

    def values(self):
        return values

    def __setitem__(self, key, value):
        if type(key)==type(1):
            # key is index 
            except IndexError:
                return None
    def __getitem__(self, key):
        if type(key)==type(1):
           # key is index so return value of nth sorted key
               return self[self.keys()[key]]
           except IndexError:
               return None
            return self.data[key]
if __name__=='__main__':
    od = OrderedDict()
    od['Alice'] = 'Anderson'
    od['Bob'] = 'Bradley'
    od['Jim'] = 'Smith'
    od['Sarah'] = 'Zac'
    od['Dave'] = 'Dobson'
    od[3] = 'Johnson'


More information about the Python-list mailing list