Why are there no ordered dictionaries?

Christoph Zwerschke cito at online.de
Mon Nov 21 12:29:21 EST 2005


Alex Martelli wrote:

> Note the plural in 'insertion orderS': some people care about the FIRST
> time a key was added to a dict, some about the LAST time it was added,
> some about the latest time it was 'first inserted' (added and wasn't
> already there) as long as it's never been deleted since that occasion --
> and these are just a few of the multifarious orders based on the time of
> insertions and deletions of keys.

Ok, I start to understand that ambiguity emerges when you delete and 
insert items. I didn't think much about this problem because  my use 
cases usually do not involve inserttion or deletion after the ordered 
dictionary has been created.

But I think the following rule is "natural" enough to consider it as THE 
standard behavior of ordered dictionaries:

"Insertion: If the key exists: Don't change the order. If it does not 
exist: Append it to the sequence of keys. Deletion: Remove from the 
sequence of keys."

I think this is also the behavior of associative arrays in PHP or Perl 
and could be considered as the "ONE unambiguous definition".

-- Christoph



More information about the Python-list mailing list