[issue34123] ambiguous documentation for dict.popitem
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
New submission from Dan Snider <mr.assume.away@gmail.com>: https://docs.python.org/3/library/stdtypes.html#dict.popitem `dict.popitem` no longer returns an "arbitrary" (key, value) pair as the documentation suggests. Rather, it always returns the pair whose key was most recently *inserted* (ie., the last entry in `dk_entries`). Perhaps the docs could reflect that this method is now always LIFO rather arbitrary now that insertion order is guaranteed? ---------- assignee: docs@python components: Documentation messages: 321708 nosy: bup, docs@python priority: normal severity: normal status: open title: ambiguous documentation for dict.popitem versions: Python 3.7, Python 3.8 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34123> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
INADA Naoki <songofacandy@gmail.com> added the comment: I think it is implementation detail yet. Only iteration order is guaranteed. ---------- nosy: +inada.naoki _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34123> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: My opinion is that it is in fact guaranteed. It makes no sense for all other aspects of the dict behavior to be guaranteed and not this one. This is what PyPy and CPython already do and there is no scenario where an implementation would be able to append a new pair at the end but unable to reverse the operation. IMO, leaving this in an ambiguous state would created problems unnecessarily and it would preclude reasonable uses of the the ordering feature. ---------- nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34123> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- assignee: docs@python -> rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34123> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- keywords: +patch pull_requests: +7826 stage: -> patch review _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34123> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
INADA Naoki <songofacandy@gmail.com> added the comment: @Armin Rigo How do you think about this? Is there no possible optimizations by breaking LIFO dict.popitem()? ---------- nosy: +arigo _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34123> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment:
Is there no possible optimizations by breaking LIFO dict.popitem()?
Even if there were a possible optimization, we wouldn't care. The API is too desirable to forgo in the name of micro-optimization. We don't design our APIs that way -- trading the relevant and actionable for ethereal and unknown. Also, it is common sense that addition of a key/value pair at the end is a readily undoable operation (no more expensive to remove than it was to add). There will be a dummy entry in the hash array just like there is now. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34123> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Armin Rigo <arigo@users.sourceforge.net> added the comment: Agreed with Raymond. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34123> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: New changeset 01b7d5898262dbe0e9edb321b3be9a34da196f6f by Raymond Hettinger in branch 'master': bpo-34123: Fix missed documentation update for dict.popitem(). (GH-8292) https://github.com/python/cpython/commit/01b7d5898262dbe0e9edb321b3be9a34da1... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34123> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>: ---------- pull_requests: +7842 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34123> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: New changeset bfa8a358e2cec40484c4655138ca3c6b10f8462a by Raymond Hettinger (Miss Islington (bot)) in branch '3.7': bpo-34123: Fix missed documentation update for dict.popitem(). (GH-8292) (GH#8307) https://github.com/python/cpython/commit/bfa8a358e2cec40484c4655138ca3c6b10f... ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34123> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Change by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue34123> _______________________________________
participants (5)
-
Armin Rigo
-
Dan Snider
-
INADA Naoki
-
miss-islington
-
Raymond Hettinger