armin.ronacher at active-4
Jun 14, 2008, 11:07 PM
Post #17 of 54
Raymond Hettinger <python <at> rcn.com> writes:
> For an insertion order dictionary, there was also a question about
> how to handle duplicate keys.
> Given odict([(k1,v1), (k2,v2), (k1,v3)]), what should the odict.items() return?
> [(k1,v3), (k2,v2)]
> [(k2,v2), (k1,v3)]
All the ordered dict implementations I saw behave like this:
>>> odict([(1, 'foo'), (2, 'bar'), (1, 'baz')]).items()
[(1, 'baz'), (2, 'bar')]
And that makes sense because it's consistent with the dict interface.
But I guess that is a pretty small issue and most of the time you are
not dealing with double keys when using an ordered dict.
> IIRC, previous discussions showed an interest in odicts but that
> there were a lot of questions of the specific semantics of the API.
No doubt there. But
> This would no doubt be compounded by attempts to emulate
> dict views. Regardless, there should probably be a PEP and
> alternate pure python versions should be posted on ASPN so people
> can try them out.
That's true, but by now there are countless of ordered dict implementations
with a mostly-compatible interface and applications and libraries are
using them already.
I have an example implementation here that incorporates the ideas
from ordereddict, Django's OrderedDict and Babel's odict:
Python-Dev mailing list
Python-Dev [at] python