diff options
author | Raymond Hettinger <python@rcn.com> | 2010-07-31 10:14:41 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-07-31 10:14:41 (GMT) |
commit | 4821ef89d4ce166c735cad48a57c03eae01fa577 (patch) | |
tree | 8eff92daa427ab88cb7419c30236177e29dc1415 | |
parent | 9e46ef819c38ec76273d7ffb35bcd14a558d35d4 (diff) | |
download | cpython-4821ef89d4ce166c735cad48a57c03eae01fa577.zip cpython-4821ef89d4ce166c735cad48a57c03eae01fa577.tar.gz cpython-4821ef89d4ce166c735cad48a57c03eae01fa577.tar.bz2 |
Document how to change OrderedDict update order from first to last.
-rw-r--r-- | Doc/library/collections.rst | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 2b85b2c..6c881f5 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -938,6 +938,18 @@ The new sorted dictionaries maintain their sort order when entries are deleted. But when new keys are added, the keys are appended to the end and the sort is not maintained. +It is also straight-forward to create an ordered dictionary variant +that the remembers the order the keys were *last* inserted. +If a new entry overwrites an existing entry, the +original insertion position is changed and moved to the end:: + + class LastUpdatedOrderedDict(OrderedDict): + 'Store items is the order the keys were last added' + def __setitem__(self, key, value): + if key in self: + del self[key] + OrderedDict.__setitem__(self, key, value) + :class:`UserDict` objects ------------------------- |