Auto sort in Dictionaries???

Steve Horne sh at ttsoftware.co.uk
Mon Jul 23 06:05:26 EDT 2001


On Mon, 23 Jul 2001 10:20:28 +0100, Steve Horne <sh at ttsoftware.co.uk>
wrote:

>You can get the keys in order if you want - just use the sort
>function. This is a little annoying, but given that Python would have
>to do the same thing to return the keys in order - inefficient for
>applications where that is not needed - it is better that it continues
>to work the way it does.

Oops - sorry, that doesn't help. I read your message as needing the
keys in sorted order - not insertion order.

To preserve the insertion order, you really need to store some kind of
sequence number.

Each time you insert a key, therefore, you could do something like...

  Dict [(Seq_No, Original_Key)] = Value
  Seq_No += 1

Or...

  Dict [Original_Key] = (Seq_No, Value)

Having done that, you'd have to derive values with the sequence
numbers at the head and sort that to get the keys in insertion order.

-- 
Steve Horne
Home : steve at lurking.demon.co.uk
Work : sh at ttsoftware.co.uk



More information about the Python-list mailing list