OrderedDict dict methods
They behave exactly like their dict namesakes — new keys go to the end, existing keys keep their place — with two OrderedDict twists: copy() and fromkeys() return an OrderedDict, and the keys/values/items views support reversed().
Common call
od.update(b=2) · od.pop('a') · list(reversed(od.items()))
Returns
what dict returns
Replaces
nothing new — they are the dict API
Watch out
update() does not move existing keys
OrderedDict.update([other], **kwds)
→ varies
Demo
Live evaluation
Existing keys get new values in place; new keys go to the end.
Try:
Inputs
keyslist[str]initial keys (value 0)
morelist[str]keys to update (value 1)
Code
from collections import OrderedDict od = OrderedDict.fromkeys(['a', 'b', 'c'], 0) od.update(dict.fromkeys(['b', 'x'], 1)) od
Result
OrderedDict({'a': 0, 'b': 1, 'c': 0, 'x': 1})
In the update tab b keeps its second place with its new value 1, and only the new key x is appended. pop of a missing key without a default is a KeyError, just like dict.pop.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| key | hashable | yes | pop / setdefault: the key. |
| default | object | no (None) | pop: returned when key is missing (otherwise KeyError); setdefault: inserted at the END when key is missing. |
| other | mapping | pairs | no | update: new keys are appended in the order given; existing keys keep their position. |
| iterable | iterable | yes | fromkeys: the keys, in order. |
Return value
varies — Same results as the dict methods of the same name; copy() and fromkeys() return an OrderedDict.
Common patterns
Newest first
Views are reversible (plain dict views are too).
for key, value in reversed(od.items()): print(key, value)
Ordered set of keys
fromkeys de-duplicates while keeping first-seen order.
from collections import OrderedDict unique = list(OrderedDict.fromkeys(items))
Pop with a fallback
pop(key, default) never raises.
value = od.pop(key, None)
Examples
1. fromkeys returns an OrderedDict
from collections import OrderedDict
OrderedDict.fromkeys('ab', 0)
Returns
OrderedDict({'a': 0, 'b': 0})2. copy keeps the type
from collections import OrderedDict
type(OrderedDict(a=1).copy()).__name__
Returns
'OrderedDict'3. Views in order
from collections import OrderedDict
od = OrderedDict(b=1, a=2)
(list(od.keys()), list(od.values()))
Returns
(['b', 'a'], [1, 2])4. Reversed items
from collections import OrderedDict
list(reversed(OrderedDict(a=1, b=2).items()))
Returns
[('b', 2), ('a', 1)]5. update keeps positions
from collections import OrderedDict
od = OrderedDict(a=1, b=2)
od.update(a=10, c=3)
od
Returns
OrderedDict({'a': 10, 'b': 2, 'c': 3})6. pop with a default
from collections import OrderedDict
OrderedDict(a=1).pop('x', 'none')
Returns
'none'7. clear
from collections import OrderedDict
od = OrderedDict(a=1)
od.clear()
od
Returns
OrderedDict()Pitfalls
1. Expecting update() to move keys to the end
Existing keys keep their position. Use move_to_end afterwards if recency matters.
update only
from collections import OrderedDict od = OrderedDict(a=1, b=2) od.update(a=3) list(od)
['a', 'b']
update + move_to_end
from collections import OrderedDict od = OrderedDict(a=1, b=2) od.update(a=3) od.move_to_end('a') list(od)
['b', 'a']
2. pop without a default
A missing key raises KeyError.
od.pop('x')
from collections import OrderedDict OrderedDict(a=1).pop('x')
KeyError: 'x'
od.pop('x', None)
from collections import OrderedDict print(OrderedDict(a=1).pop('x', None))
None
When to use
Use it
- Any normal dict work on an OrderedDict
Reach for something else
- Reordering → move_to_end; removing from the front → popitem(last=False)
Notes
CPython impl
Objects/odictobject.c defines its own keys/values/items views and update/pop/setdefault/clear/copy/fromkeys so the linked list of keys stays in sync with the dict
Versions
Reverse iteration of the keys/values/items views since 3.5 (docs.python.org)
FAQ
No. Like dict.update, it replaces the value of an existing key in place and appends only new keys, in the order they are given.