Sequenced dictionaries

Emile van Sebille emile at fenx.com
Mon Nov 5 15:12:33 CET 2001


"Morten W. Petersen" <morten at thingamy.net> wrote in message
news:Pine.LNX.4.21.0111051436400.21948-100000 at bcryachts.atsat.com...
> Hi,
>
> I'm wondering if there exists any C-based implementations of sequenced
> dictionaries, i.e. dictionaries that will remember the order of the
> objects 'added' on it.
>
> I have a pure python implementation [1] but suspect that it will be
> too slow..

Note that in 2.2 you'll be able to do the following:

Python 2.2a2+ (#22, Sep  5 2001, 14:10:41) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.

>>> class seqdict(dictionary):
 def __init__(self):
  self.seq = []
  dictionary.__init__(self)
 def __setitem__(self, ky, val):
  self.seq.append((ky, val))
  dictionary.__setitem__(self, ky, val)


>>> d = seqdict()
>>> d[3] = 3
>>> d[2] = 2
>>> d[1] = 1
>>> d['c'] = 1
>>> d['b']=2
>>> d['a']=1
>>> d.seq
[(3, 3), (2, 2), (1, 1), ('c', 1), ('b', 2), ('a', 1)]
>>> d
{'a': 1, 1: 1, 2: 2, 3: 3, 'b': 2, 'c': 1}
>>>

Although dictionary wil be renamed dict for the final release.

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list