Performance ordered dictionary vs normal dictionary

Dan Stromberg drsalists at gmail.com
Sat Jul 31 00:18:20 EDT 2010


On Wed, Jul 28, 2010 at 11:11 PM, Chris Rebert <clp2 at rebertia.com> wrote:

> On Wed, Jul 28, 2010 at 8:12 PM, Navkirat Singh <navkirats at gmail.com>
> wrote:
> > Sorry, I might have been a bit vague:
> > (Also, I am new to pythong)
> > I am trying to do construct my own web session tracking algorithm for a
> web
> > server (which also I have constructed). The book keeping is for the
> session
> > information I track. The dictionary object will do this for me. I was
> > initially using a plain dictionary object, but I read this in the
> > documentation that made me think about the question I asked :-
>
> Does your program actually make use of the ordering? If so, then yes,
> using OrderedDict is obviously preferable (Why reimplement something
> needlessly?). If not, then why bother with the extra complication?
>
> (You are aware that the "ordered" in OrderedDict means that its keys
> are ordered, and not that, say, a list containing OrderedDicts can be
> sorted, right?)


Actually, a collections.OrderedDict saves things not in key order, but in
chronological order:

$ /usr/local/python27/bin/python
Python 2.7rc1 (r27rc1:81772, Jun 10 2010, 14:28:26)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import collections
>>> d = collections.OrderedDict()
>>> d
OrderedDict()
>>> d[5] = 'a'
>>> d[3] = 'b'
>>> d[8] = 'c'
>>> print d.keys()
[5, 3, 8]
>>>

If you want things saved in key order, you might try my treap or duptreap
module:

http://stromberg.dnsalias.org/~dstromberg/treap/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100730/8393d32f/attachment-0001.html>


More information about the Python-list mailing list