[Python-ideas] OrderedDict.insert()
Thomas Güttler
guettliml at thomas-guettler.de
Thu Mar 26 09:59:06 CET 2015
Am 25.03.2015 um 20:59 schrieb Ron Adam:
>
>
> On 03/25/2015 12:51 PM, Andrew Barnert wrote:
>> On Mar 25, 2015, at 04:00, Thomas Güttler<guettliml at thomas-guettler.de>
>> wrote:
>
>>>> I am missing OrderedDict.insert()
>
>> With what interface? Does it take an index, like list.insert, even
>> though other OrderedDict methods (including __getitem__ and friends)
>> don't? Or does it take another key? If so, does it insert before or
>> after that key? And what if that key doesn't exist? Meanwhile, if the
>> new key was already in the dict, should it move to the new position,
>> update values in place, or raise an exception? Do you expect O(1)
>> performance?
My use case is that django drops support for SortedDict, and you should use
collection.OrderedDict in the future:
https://code.djangoproject.com/wiki/SortedDict
We have one line where we use my_ordered_dict.insert() and this line is already updated.
The issue is not very important for me.
To your question:
The interface of django was insert(index, key, value) but you are right. An interface
which is relative to an existing key would be better.
insert_before(other_key, new_key, new_value)
insert_behind(other_key, new_key, new_value)
Both methods should raise KeyError if other_key is not in the dict and
should raise KeyError if new_key is already in the dict.
Performance: I don't care. I store big data in a database. I don't carry it
around in the memory if the python interpreter.
Regards,
Thomas Güttler
More information about the Python-ideas
mailing list