Why are there no ordered dictionaries?

Bengt Richter bokr at oz.net
Tue Nov 22 16:52:12 EST 2005


On Tue, 22 Nov 2005 21:24:29 +0100, Christoph Zwerschke <cito at online.de> wrote:

>Carsten Haese wrote:
>
>> That could easily be fixed by making the sequence a "managed property"
>> whose setter raises a ValueError if you try to set it to something
>> that's not a permutation of what it was.
>
>Ok, a managed attribute would be an option. You would allow people to do 
>what they want with the sequence, and then fix the dictionary 
>accordingly (if items were deleted from the sequence, they are deleted 
>from the dictionary, it items were added which are not in the directory, 
>a ValueError is raised etc.).
>
>But probably it is still better to hide the sequence and instead of 
>letting people do list operations like sort() on the odict.sequence, let 
>them do these operations on odict directly.
>
>>>d1[0] + d1[2] ==> OrderedDict( (1, 11), (3, 13) )
>> What do you think you're doing here?
>
>Sorry, what I meant was
>
>d1[0:0] + d1[2:2] ==> OrderedDict( (1, 11), (3, 13) )
>
>Ordered dictionaries could allow slicing and concatenation.
Those are zero-length slices in normal notation. ITYM [0:1] and [2:3]?
>
Note that you'd have to define addition as possible replacement,
if all the keys happened to match. Or pure concat if none matched,
and variations mixing both ways.
But with the current version you can already write that as

    OrderedDict(d1.items()[0:1]+d2.items()[2:3])

you just want the sugar? d1+d2 would be like using [:] in the above line
Not a biggie to do.

Regards,
Bengt Richter



More information about the Python-list mailing list