Python OrderedDict
Clear, practical guide to Python OrderedDict: behavior, methods (move_to_end, popitem), examples, equality rules, and when to use OrderedDict vs dict.
Drake Nguyen
Founder · System Architect
Python OrderedDict: overview and use cases
Python OrderedDict is a dict subclass from the Python collections module that preserves the order in which keys are inserted. While modern Python (3.7+) guarantees insertion order for built-in dict objects, OrderedDict remains useful when you need order-sensitive behavior or specialized methods such as move_to_end and popitem with FIFO/LIFO control.
Why choose OrderedDict over dict?
- OrderedDict preserves insertion order and exposes extra OrderedDict methods for ordering control.
- You can explicitly move keys to the beginning or end without rebuilding the mapping.
- popitem supports both FIFO and LIFO semantics, making it handy for queue/stack-like patterns.
- Equality between two OrderedDict instances is order-sensitive, which can be useful for tests where key order matters.
Key OrderedDict features
- Part of the Python collections module and implemented as a dict subclass.
- Methods that affect position: move_to_end(key, last=True), popitem(last=True).
- Regular mapping operations (get, set, pop, update) behave like dict while retaining order semantics.
- Reverse iteration using reversed(ordered_dict) returns keys from last to first.
- When you delete and reinsert a key, it moves to the end (new insertion position).
Python OrderedDict examples
Below are practical examples showing common patterns and the behavior differences between OrderedDict and dict.
Creating an OrderedDict
from collections import OrderedDict
# start with an empty OrderedDict
od = OrderedDict()
od['kiwi'] = 4
od['apple'] = 5
od['cat'] = 3
# create from another mapping
base = {'kiwi': 4, 'apple': 5, 'cat': 3}
od_from = OrderedDict(base)
print(od)
print(od_from)
Adding, replacing and reinserting keys
# replacing a value keeps its original position
od['kiwi'] = 10 # 'kiwi' remains where it was
# deleting then re-adding moves the key to the end
od.pop('kiwi')
od['kiwi'] = 4 # now 'kiwi' is the last item
print(list(od.items()))
Using move_to_end
# move a key to the end (default)
od.move_to_end('apple')
# move a key to the beginning
od.move_to_end('dog', last=False)
print(list(od.keys()))
popitem: FIFO vs LIFO
# remove and return the last item (LIFO)
last_key, last_val = od.popitem()
# remove and return the first item (FIFO)
first_key, first_val = od.popitem(last=False)
Reverse iteration
# iterate keys in reverse insertion order
for key in reversed(od):
print(key, od[key])
Equality semantics
Two OrderedDict objects are considered equal only if they contain the same key/value pairs in the same order. When compared to a regular mapping, equality ignores order (mapping-level equality), so OrderedDict can still be used where a dict is expected.
d1 = {'a': 1, 'b': 2}
d2 = {'b': 2, 'a': 1}
od1 = OrderedDict([('a', 1), ('b', 2)])
od2 = OrderedDict([('b', 2), ('a', 1)])
print(d1 == d2) # True (mapping equality, order ignored)
print(od1 == od2) # False (order-sensitive)
print(d1 == od1) # True (OrderedDict compares equal to mapping if keys/values match)
When to use OrderedDict: practical guidance
- Use OrderedDict when the order of keys matters for algorithms, caching, or deterministic output.
- If you need to efficiently move keys to front/end or implement LRU-like caches, OrderedDict methods simplify the logic.
- For many cases where only insertion order matters and no reordering is needed, the built-in dict in Python 3.7+ is sufficient.
Common OrderedDict methods
- move_to_end(key, last=True) — move key to end or beginning.
- popitem(last=True) — pop last (LIFO) by default, or first (FIFO) with last=False.
- pop(key, default) — remove and return value for key.
- update(mapping) — update from another mapping while preserving order of added keys.
Tip: To implement an LRU cache, combine OrderedDict with move_to_end on access and popitem(last=False) to evict the oldest entry.
These examples and descriptions illustrate how Python OrderedDict lets you control and inspect insertion order while offering convenient, order-aware methods not available on plain dict objects.