summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorINADA Naoki <methane@users.noreply.github.com>2018-06-04 22:09:22 (GMT)
committerNed Deily <nad@python.org>2018-06-04 22:09:22 (GMT)
commitf822549653d8d09bffff5b7dcddfdf12679a787c (patch)
tree4bea6fc6f817e4aee0d1f43c233a9f0ffca5e040 /Doc
parent2a4a62ba4ae770bbc7b7fdec0760031c83fe1f7b (diff)
downloadcpython-f822549653d8d09bffff5b7dcddfdf12679a787c.zip
cpython-f822549653d8d09bffff5b7dcddfdf12679a787c.tar.gz
cpython-f822549653d8d09bffff5b7dcddfdf12679a787c.tar.bz2
bpo-33609: Document dict insertion order guarantee as of 3.7 (GH-7093)
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/stdtypes.rst23
1 files changed, 23 insertions, 0 deletions
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index af2b4e1..80dfe88 100644
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -4248,6 +4248,29 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
value)`` pairs. Order comparisons ('<', '<=', '>=', '>') raise
:exc:`TypeError`.
+ Dict preserves insertion order. Note that updating key doesn't affects the
+ order. On the other hand, keys added after deletion are inserted to the
+ last. ::
+
+ >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
+ >>> d
+ {'one': 1, 'two': 2, 'three': 3, 'four': 4}
+ >>> list(d)
+ ['one', 'two', 'three', 'four']
+ >>> list(d.values())
+ [1, 2, 3, 4]
+ >>> d["one"] = 42
+ >>> d
+ {'one': 42, 'two': 2, 'three': 3, 'four': 4}
+ >>> del d["two"]
+ >>> d["two"] = None
+ >>> d
+ {'one': 42, 'three': 3, 'four': 4, 'two': None}
+
+ .. versionchanged:: 3.7
+ Dict order is guaranteed to be insertion order. This behavior was
+ implementation detail of CPython from 3.6.
+
.. seealso::
:class:`types.MappingProxyType` can be used to create a read-only view
of a :class:`dict`.