Skip to content

Commit 8585ec9

Browse files
bpo-33609: Document dict insertion order guarantee as of 3.7 (GH-7093)
(cherry picked from commit f822549) Co-authored-by: INADA Naoki <[email protected]>
1 parent 103058e commit 8585ec9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Doc/library/stdtypes.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4229,6 +4229,29 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
42294229
value)`` pairs. Order comparisons ('<', '<=', '>=', '>') raise
42304230
:exc:`TypeError`.
42314231

4232+
Dict preserves insertion order. Note that updating key doesn't affects the
4233+
order. On the other hand, keys added after deletion are inserted to the
4234+
last. ::
4235+
4236+
>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4237+
>>> d
4238+
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
4239+
>>> list(d)
4240+
['one', 'two', 'three', 'four']
4241+
>>> list(d.values())
4242+
[1, 2, 3, 4]
4243+
>>> d["one"] = 42
4244+
>>> d
4245+
{'one': 42, 'two': 2, 'three': 3, 'four': 4}
4246+
>>> del d["two"]
4247+
>>> d["two"] = None
4248+
>>> d
4249+
{'one': 42, 'three': 3, 'four': 4, 'two': None}
4250+
4251+
.. versionchanged:: 3.7
4252+
Dict order is guaranteed to be insertion order. This behavior was
4253+
implementation detail of CPython from 3.6.
4254+
42324255
.. seealso::
42334256
:class:`types.MappingProxyType` can be used to create a read-only view
42344257
of a :class:`dict`.

0 commit comments

Comments
 (0)