Efficiently building ordered dict

John Posner jjposner at optimum.net
Tue Feb 23 00:34:58 CET 2010


On 2/22/2010 4:29 PM, Bryan wrote:
>
> Sorry about the sorted != ordered mix up.  I want to end up with a
> *sorted* dict from an unordered list.  *Sorting the list is not
> practical in this case.*  I am using python 2.5, with an ActiveState
> recipe for an OrderedDict.
>

Have you looked at this:

   http://pypi.python.org/pypi/sorteddict/1.2.1

 >>> data = zip('afcedbijhg', range(10))
 >>> old = dict(data)
 >>> for key in old:
...     print key, old[key]
...
a 0
c 2
b 5
e 3
d 4
g 9
f 1
i 6
h 8
j 7
 >>> new = sorteddict.sorteddict(data)
 >>> for key in new:
...     print key, new[key]
...
a 0
b 5
c 2
d 4
e 3
f 1
g 9
h 8
i 6
j 7

-John



More information about the Python-list mailing list