'insert', 'swap', 'get_by_index' for `OrderedDict`

OrderedDict is really an implementation of a circular doubly linked list with key-based mapping access, so a get_by_index method makes zero sense in terms of efficiency.

But I do support the OP’s idea of insert and swap methods since there are perfect use cases for them in queue management where a mapped doubly linked list is the most efficient data structure.

The insert method by the way should really be two methods, insert_before(ref_key, new_key, value) and insert_after(ref_key, new_key, value).

It will also satisfy recurring calls (like this, this and that) for a built-in data type of a doubly linked list on this forum, especially if we add two more methods next(key) and prev(key). Also nice to have an append_left (or append_first) method instead of having to assign by key and then move_to_end(key, last=False).

All of the methods mentioned above can be trivially implemented since the hard work has already been done.

OrderedDict’s usage has dwindled ever since dict became ordered so I share the OP’s sentiment that OrderedDict should justify its existence with broadened capabilities. The benefit-to-cost ratio in this case is just too good not to do it IMHO.

1 Like