See Ash Code logo See Ash Code
Completed Creator

IndexedDict

A Python dictionary with methods for accessing and rearranging items by position.

Python Data structures Packaging

A mapping where position matters

Python dictionaries preserve insertion order, but most of their public interface is about keys. Sometimes a program needs both: it needs to retrieve a value by name and also insert, move, or remove an item at a specific position.

I built IndexedDict to provide those operations in a Python mapping. The package supports ordinary key-based access as well as methods such as position, get_from_index, insert, move_to_index, and pop_from_index. It is available as indexed-dict on PyPI, with the source on GitHub.

The distinction from a standard dictionary is not that it remembers insertion order. Python already does that. The useful addition is an interface for acting on that order.

Representing order and lookup together

IndexedDict implements MutableMapping. Internally it maintains an ordered list of keys and a dictionary that maps each key to a small node holding its value and current index.

ordered keys:  ["draft", "review", "publish"]
                    ↓         ↓         ↓
key lookup:    draft → node(value, index=0)
               review → node(value, index=1)
               publish → node(value, index=2)

Looking up a key uses the dictionary. Finding a key at a position uses the list. Finding a key’s position reads the index stored in its node. A refactor in the project’s history introduced the node representation to avoid having to search the ordered keys just to recover a known key’s position.

There is a cost: the list and the nodes describe the same ordering, so mutations must keep them in agreement. The useful invariant is that every key in the ordered list has a corresponding node, and that node’s stored index matches the key’s actual position.

Moving and inserting without losing the mapping

Consider a workflow with three entries: draft, review, and publish. Moving publish to the beginning should change their order without changing the value associated with any key.

from indexed_dict import IndexedDict

steps = IndexedDict({"draft": 1, "review": 2, "publish": 3})
steps.move_to_index("publish", 0)

list(steps.keys())          # ["publish", "draft", "review"]
steps.position("review")    # 2
steps["publish"]            # 3

The implementation removes the key from the ordered list, inserts it at the new position, and resynchronizes affected node indices. Insertion and deletion have similar bookkeeping. Tests cover moves to the beginning, end, same position, negative positions, and missing keys, as well as preserving the moved value.

Fast lookup does not make every operation fast. A list insertion or removal can shift items, and resynchronizing indices can touch the affected tail. The implementation favors straightforward key and positional access while accepting linear work for changes to the order. That is a useful trade-off for some collections, but it is worth considering for workloads dominated by frequent rearrangement of very large mappings.

A mapping interface with list-like operations

The package retains familiar mapping operations including iteration, membership, update, pop, views, and conversion back to a regular dictionary. It adds slicing for ordered values. Slice assignment replaces values for the existing keys and rejects a length mismatch rather than silently adding or dropping entries; slice deletion removes the selected keys.

A positional lookup uses an explicit method such as get_from_index(1). That matters for mappings whose keys are themselves integers: mapping[1] can remain a key lookup without guessing whether the caller meant a position.

The separate structures make edge cases important. After an insertion, deletion, slice operation, sort, or move, iteration order and stored positions must still agree. The repository organizes tests by operation and includes cases for invalid slice assignments and negative indices. Its test configuration requires at least 90% coverage, and its GitHub Actions workflow runs a Python 3.10–3.13 matrix before the release job. Those are configured checks; they aren’t a benchmark or a claim about adoption.

What the project demonstrates

IndexedDict is a small library, but its central problem is a useful one: one piece of data has two ways to be addressed, and both must remain consistent when it changes. The design makes that relationship explicit, documents the cost of maintaining it, and gives users a mapping interface they can use without maintaining their own parallel list of keys.