OrderedDict
OrderedDict is a dictionary subclass from the collections module that remembers the order in which keys were inserted. from collections import OrderedDict;...
What is OrderedDict?
OrderedDict is a dictionary subclass from the collections module that remembers the order in which keys were inserted. from collections import OrderedDict; od = OrderedDict(); od["b"] = 2; od["a"] = 1; list(od.keys()) gives ["b", "a"] — insertion order. In Python 3.7+, the built-in dict also maintains insertion order, which means OrderedDict is less necessary for order preservation. However, OrderedDict still has unique features that dict lacks.
Unique Features of OrderedDict
OrderedDict supports move_to_end(key, last=True) which moves a key to the end (or beginning if last=False): od.move_to_end("b") moves "b" to the last position. popitem(last=True) removes and returns the last inserted item (LIFO) or the first (FIFO) if last=False. OrderedDict also has richer equality comparison: two OrderedDicts with the same key-value pairs but in different order are not equal, unlike regular dicts where order does not affect equality.
LRU Cache with OrderedDict
A classic OrderedDict use case is implementing an LRU (Least Recently Used) cache. When a key is accessed, move it to the end; when the cache is full and a new key arrives, remove the first key (the least recently used). class LRUCache: def __init__(self, cap): self.cache = OrderedDict(); self.cap = cap. The get method calls move_to_end on access; the put method appends and calls popitem(last=False) when capacity is exceeded. This is an extremely common interview problem.
OrderedDict vs dict in Python 3.7+
Since Python 3.7, regular dicts maintain insertion order as a language guarantee (not just an implementation detail). For most use cases, dict is sufficient. Choose OrderedDict when: you need move_to_end(), you need order-sensitive equality, you are writing code that must run on Python versions below 3.7, or you explicitly want to signal that order is semantically meaningful to readers of your code. In all other cases, a regular dict is simpler and slightly more memory-efficient.
Worked Example
Deduplicate a list while preserving order: from collections import OrderedDict; def dedupe(lst): return list(OrderedDict.fromkeys(lst)). dedupe([3, 1, 2, 1, 3, 4]) gives [3, 1, 2, 4]. The fromkeys() class method creates an OrderedDict from an iterable of keys (values default to None), and the dict's key uniqueness removes duplicates while maintaining first-seen order. In Python 3.7+ you can also use dict.fromkeys() directly since order is preserved.
Exam Tip
OrderedDict exam questions tend to focus on the distinction from plain dict. What does OrderedDict add over dict in Python 3.7+? move_to_end(), order-sensitive equality, and popitem(last=False). What is fromkeys()? A class method that creates a dict/OrderedDict from an iterable of keys with optional default values. Common interview task: implement an LRU cache in Python — the intended answer uses OrderedDict with move_to_end() and popitem(last=False). Being able to write this from scratch demonstrates both data structure knowledge and Python library knowledge.