OrderedDict.move_to_end
The operation that makes OrderedDict worth using: O(1) reordering of one key to either end. The key must exist — there is no "insert at the front".
Common call
od.move_to_end('a') · od.move_to_end('a', last=False)
Returns
None
Replaces
d[k] = d.pop(k) on a plain dict (end only)
Watch out
KeyError when the key is missing
OrderedDict.move_to_end(keykey — An existing key.type: hashable · required, lastlast — True: move to the end. False: move to the front.type: bool · default: True=True)
→ None
Demo
Live evaluation
move_to_end(key) — the key becomes the last one.
Try:
Inputs
keyslist[str]keys in order
keystrkey to move
Code
from collections import OrderedDict od = OrderedDict.fromkeys(['a', 'b', 'c']) od.move_to_end('a') list(od)
Result
['b', 'c', 'a']
Moving a key that is already at the requested end changes nothing. A key that does not exist raises KeyError with the key as the message — move_to_end never inserts.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| key | hashable | yes | An existing key. |
| last | bool | no (True) | True: move to the end. False: move to the front. |
Return value
None — The OrderedDict is reordered in place.
Common patterns
Mark as recently used
LRU caches move a key to the end on every hit.
def lookup(cache, key): if key in cache: cache.move_to_end(key) return cache[key]
Pin a key to the top
Insert, then move it to the front.
od['header'] = value od.move_to_end('header', last=False)
Examples
1. Move to the end
from collections import OrderedDict
od = OrderedDict.fromkeys('abcde')
od.move_to_end('b')
''.join(od)
Returns
'acdeb'2. Move to the front
from collections import OrderedDict
od = OrderedDict.fromkeys('abcde')
od.move_to_end('b', last=False)
''.join(od)
Returns
'bacde'3. The value comes along
from collections import OrderedDict
od = OrderedDict(a=1, b=2)
od.move_to_end('a')
od
Returns
OrderedDict({'b': 2, 'a': 1})4. Missing key
from collections import OrderedDict
OrderedDict(a=1).move_to_end('x')
Returns
KeyError: 'x'5. Returns None
from collections import OrderedDict
print(OrderedDict(a=1).move_to_end('a'))
Returns
NonePitfalls
1. Using it to insert
The key must already exist. Assign first, then move.
move a new key
from collections import OrderedDict od = OrderedDict(b=2) od.move_to_end('a', last=False)
KeyError: 'a'
assign, then move
from collections import OrderedDict od = OrderedDict(b=2) od['a'] = 1 od.move_to_end('a', last=False) od
OrderedDict({'a': 1, 'b': 2})
2. Expecting assignment to reorder
Updating the value of an existing key keeps its position, in dicts and OrderedDicts alike.
od[k] = v
from collections import OrderedDict od = OrderedDict(a=1, b=2) od['a'] = 9 list(od)
['a', 'b']
then move_to_end
from collections import OrderedDict od = OrderedDict(a=1, b=2) od['a'] = 9 od.move_to_end('a') list(od)
['b', 'a']
When to use
Use it
- Recency ordering (LRU, MRU lists)
- Pinning or sinking one entry
Reach for something else
- Sorting everything → build a new dict from sorted(d.items())
Notes
CPython impl
OrderedDict_move_to_end_impl in Objects/odictobject.c — relinks one node of the internal linked list
Versions
Added in 3.2 (docs.python.org)
FAQ
With an OrderedDict: od.move_to_end(key). With a plain dict: d[key] = d.pop(key) re-inserts it at the end.