summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-07-31 10:16:57 (GMT)
committerRaymond Hettinger <python@rcn.com>2010-07-31 10:16:57 (GMT)
commit5a34b7f50a822fbc1b1f9dfb429b96e3df92928b (patch)
treeb6085ebfdeeaaa929aa020575f23f1d2648b74a6 /Doc/library
parent908cb7db8c840c8839d1ec5bfe69d734fbac8456 (diff)
downloadcpython-5a34b7f50a822fbc1b1f9dfb429b96e3df92928b.zip
cpython-5a34b7f50a822fbc1b1f9dfb429b96e3df92928b.tar.gz
cpython-5a34b7f50a822fbc1b1f9dfb429b96e3df92928b.tar.bz2
Document how to change OrderedDict update order from first to last.
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/collections.rst12
1 files changed, 12 insertions, 0 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index 29504e0..d2aa0cc 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -911,6 +911,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
-------------------------