summaryrefslogtreecommitdiffstats
path: root/Doc/library/collections.rst
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-02-25 15:15:58 (GMT)
committerGitHub <noreply@github.com>2022-02-25 15:15:58 (GMT)
commit8b37a0c522a11bd4a1ae66a132fa6c2789c65213 (patch)
tree63472019b5fff3cb73a7e64b2a2e6ab982580fd0 /Doc/library/collections.rst
parent0848da19ce8ea037ab1cfc569778e94bf8e3b24a (diff)
downloadcpython-8b37a0c522a11bd4a1ae66a132fa6c2789c65213.zip
cpython-8b37a0c522a11bd4a1ae66a132fa6c2789c65213.tar.gz
cpython-8b37a0c522a11bd4a1ae66a132fa6c2789c65213.tar.bz2
Update dict/OrderedDict differences with code equivalents. (GH-31563)
(cherry picked from commit 26aba295a9c1bcb0812fe44bd7e68ddd1d8a6828) Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
Diffstat (limited to 'Doc/library/collections.rst')
-rw-r--r--Doc/library/collections.rst29
1 files changed, 23 insertions, 6 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index c45d90c..67b64dd 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -1092,18 +1092,35 @@ Some differences from :class:`dict` still remain:
Space efficiency, iteration speed, and the performance of update
operations were secondary.
-* Algorithmically, :class:`OrderedDict` can handle frequent reordering
- operations better than :class:`dict`. This makes it suitable for tracking
- recent accesses (for example in an `LRU cache
- <https://medium.com/@krishankantsinghal/my-first-blog-on-medium-583159139237>`_).
+* The :class:`OrderedDict` algorithm can handle frequent reordering operations
+ better than :class:`dict`. As shown in the recipes below, this makes it
+ suitable for implementing various kinds of LRU caches.
* The equality operation for :class:`OrderedDict` checks for matching order.
+ A regular :class:`dict` can emulate the order sensitive equality test with
+ ``p == q and all(k1 == k2 for k1, k2 in zip(p, q))``.
+
* The :meth:`popitem` method of :class:`OrderedDict` has a different
signature. It accepts an optional argument to specify which item is popped.
-* :class:`OrderedDict` has a :meth:`move_to_end` method to
- efficiently reposition an element to an endpoint.
+ A regular :class:`dict` can emulate OrderedDict's ``od.popitem(last=True)``
+ with ``d.popitem()`` which is guaranteed to pop the rightmost (last) item.
+
+ A regular :class:`dict` can emulate OrderedDict's ``od.popitem(last=False)``
+ with ``(k := next(iter(d)), d.pop(k))`` which will return and remove the
+ leftmost (first) item if it exists.
+
+* :class:`OrderedDict` has a :meth:`move_to_end` method to efficiently
+ reposition an element to an endpoint.
+
+ A regular :class:`dict` can emulate OrderedDict's ``od.move_to_end(k,
+ last=True)`` with ``d[k] = d.pop(k)`` which will move the key and its
+ associated value to the rightmost (last) position.
+
+ A regular :class:`dict` does not have an efficient equivalent for
+ OrderedDict's ``od.move_to_end(k, last=False)`` which moves the key
+ and its associated value to the leftmost (first) position.
* Until Python 3.8, :class:`dict` lacked a :meth:`__reversed__` method.