Ordered dict by default

Duncan Booth duncan.booth at invalid.invalid
Thu Feb 5 12:08:09 CET 2009


Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:

> bearophileHUGS at lycos.com writes:
>> Now Ruby dicts are ordered by default:
>> http://www.igvita.com/2009/02/04/ruby-19-internals-ordered-hash/
> 
> Maybe I didn't read that carefully enough, but it looks like "ordered"
> means the dict records come out in the same order you inserted them
> in.  That is if you insert B,A,D,C in that order, you get them out in
> that order.  I would have thought an ordered dict meant you get A,B,C,D,
> which seems a lot more useful.
> 
If you want to write doctests then any stable order in the default dict 
type would be helpful no matter whether it means that keys are in original 
insertion or latest insertion order or sorted.

There are other use cases where maintaining insertion order is important. 
For example any automated code generator which parses its previous output 
and then regenerates it maintaining edits. I ran into that before with one 
which just stored the methods in a Python dict so that every so often you'd 
find the entire source file had rearranged itself making a mess of version 
control.

It would certainly be an interesting experiment to mimic the Ruby dict 
implementation in Python's dict code and see what effect it has on 
performance: the Ruby page claims that while inserts are slower traversal 
is much faster. I think it would need some pretty convincing arguments 
though to become the default.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list