summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRémi Lapeyre <remi.lapeyre@henki.fr>2018-11-06 00:38:54 (GMT)
committerINADA Naoki <methane@users.noreply.github.com>2018-11-06 00:38:54 (GMT)
commit6531bf6309c8fda1954060a0fb5ea930b1efb656 (patch)
tree64debbc908a8d0dbd043d3114a536a750bb70339 /Doc
parent16c8a53490a22bd4fcde2efaf4694dd06ded882b (diff)
downloadcpython-6531bf6309c8fda1954060a0fb5ea930b1efb656.zip
cpython-6531bf6309c8fda1954060a0fb5ea930b1efb656.tar.gz
cpython-6531bf6309c8fda1954060a0fb5ea930b1efb656.tar.bz2
bpo-33462: Add __reversed__ to dict and dict views (GH-6827)
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/stdtypes.rst29
-rw-r--r--Doc/whatsnew/3.8.rst3
2 files changed, 32 insertions, 0 deletions
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index 4686cec..49d4338 100644
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -4285,6 +4285,11 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
LIFO order is now guaranteed. In prior versions, :meth:`popitem` would
return an arbitrary key/value pair.
+ .. describe:: reversed(d)
+
+ Return a reversed iterator over the keys of the dictionary. This is a
+ shortcut for ``reversed(d.keys())``.
+
.. method:: setdefault(key[, default])
If *key* is in the dictionary, return its value. If not, insert *key*
@@ -4332,6 +4337,22 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
Dictionary order is guaranteed to be insertion order. This behavior was
implementation detail of CPython from 3.6.
+ Dictionaries and dictionary views are reversible. ::
+
+ >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
+ >>> d
+ {'one': 1, 'two': 2, 'three': 3, 'four': 4}
+ >>> list(reversed(d))
+ ['four', 'three', 'two', 'one']
+ >>> list(reversed(d.values()))
+ [4, 3, 2, 1]
+ >>> list(reversed(d.items()))
+ [('four', 4), ('three', 3), ('two', 2), ('one', 1)]
+
+ .. versionchanged:: 3.8
+ Dictionaries are now reversible.
+
+
.. seealso::
:class:`types.MappingProxyType` can be used to create a read-only view
of a :class:`dict`.
@@ -4375,6 +4396,14 @@ support membership tests:
Return ``True`` if *x* is in the underlying dictionary's keys, values or
items (in the latter case, *x* should be a ``(key, value)`` tuple).
+.. describe:: reversed(dictview)
+
+ Return an reversed iterator over the keys, values or items of the dictionnary.
+ The view will be iterated in reverse order of the insertion.
+
+ .. versionchanged:: 3.8
+ Dictionary views are now reversible.
+
Keys views are set-like since their entries are unique and hashable. If all
values are hashable, so that ``(key, value)`` pairs are unique and hashable,
diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst
index 566c369..2d3a116 100644
--- a/Doc/whatsnew/3.8.rst
+++ b/Doc/whatsnew/3.8.rst
@@ -98,6 +98,9 @@ Other Language Changes
* Added support of ``\N{name}`` escapes in :mod:`regular expressions <re>`.
(Contributed by Jonathan Eunice and Serhiy Storchaka in :issue:`30688`.)
+* Dict and dictviews are now iterable in reversed insertion order using
+ :func:`reversed`. (Contributed by Rémi Lapeyre in :issue:`33462`.)
+
* The syntax allowed for keyword names in function calls was further
restricted. In particular, ``f((keyword)=arg)`` is no longer allowed. It was
never intended to permit more than a bare name on the left-hand side of a