6

I've seen (and written) a number of implementations of this. Is there one that is considered the best or is emerging as a standard?

What I mean by ordered dict is that the object has some concept of the order of the keys in it, similar to an array in PHP.

odict from PEP 372 seems like a strong candidate, but it's not totally clear that it is the winner.

Josh Gibson
  • 21,808
  • 28
  • 67
  • 63

4 Answers4

12

This one by Raymond Hettinger is a drop-in substitute for the collections.OrderedDict that will appear in Python 2.7: http://pypi.python.org/pypi/ordereddict

The dev version of the collections docs say it's equivalent to what will be in Python 2.7, so it's probably pretty likely to be a smooth transition to the one that will come with Python.

I've put it in PyPI, so you can install it with easy_install ordereddict, and use it like so:

from ordereddict import OrderedDict
d = OrderedDict([("one", 1), ("two", 2)])
sah
  • 476
  • 4
  • 9
  • the ordereddict by Raymond Hettinger looks shorter and more elegant than collections.OrderedDict. I was thinking what in collections.OrderedDict is better than Raymond's one. How come the one in the standard package is not as good as the other? – FrostNovaZzz Dec 26 '12 at 11:14
  • @FrostNovaZzz collections.OrderedDict has seen far more use by now, so I expect the "inelegant" differences are actually bug fixes. – shoyer Nov 19 '14 at 01:28
8

I haven't seen a standard; everyone seems to roll their own (see answers to this question). If you can use the OrderedDict patch from PEP 372, that's your best bet. Anything that's included in the stdlib has a very high chance of being what everyone uses a year or two from now.

Community
  • 1
  • 1
DNS
  • 37,249
  • 18
  • 95
  • 132
2

Python 2.7 and later have OrderedDict in the collections module, so you should consider that as 'standard'. If its functionality is enough you should probably be using that.

However its implementation approach is minimalistic and if that is not enough you should look at odict by Foord/Larossa or ordereddict (by me) as in that case those are a better fit. Both implementations are a superset of the functionality provided by collections.OrderedDict. The difference between the two being, that odict is pure python and ordereddict a much faster C extension module.

A minimalistic approach is not necessarily better even if it provides all the functionality you need: e.g. collections.OrderedDict did initially have a bug when returning the repr() of a OrderedDict nested in one of its own values. A bug that could have been found earlier, had the subset, the small subset OrderedDict can handle, of unittests of the older ordereddict been used.

Anthon
  • 69,918
  • 32
  • 186
  • 246
  • thanks for your work, Anthon! I was trying to get your ordereddict, but both the zip and tar archives on your website were dead. Additionally, I am a little worried is this project still alive? I mean, will it be updated with future Python? Also, maybe, putting it on pip could make it more popular? I see 7 times speed up on your website, not sure why Python developers didn't elect this implementation as the python's default. – jichi Jan 15 '14 at 12:09
  • @jichi I am in the process of moving `ordereddict` to [bitbucket](https://bitbucket.org/ruamel/ordereddict) and make it installable with `pip`. After tat I will also look at Python 3.X compatibility (maybe based on a stripped down version, with functionality more like collections.OrderedDict). The links on the website should work again. – Anthon Jan 15 '14 at 13:02
  • May I know the license of this library? Maybe, you can add a license to the PKG-INFO file? – jichi Jan 16 '14 at 00:50
2

collections.OrderedDict should now be widely available, but if performance is concern, you might consider using my package cyordereddict as an alternative. It's a direct port of standard library's OrderedDict to Cython that is 2-6x faster.

shoyer
  • 9,165
  • 1
  • 37
  • 55