Skip to content

Commit f822549

Browse files
methanened-deily
authored andcommitted
bpo-33609: Document dict insertion order guarantee as of 3.7 (GH-7093)
1 parent 2a4a62b commit f822549

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
@@ -4248,6 +4248,29 @@ pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098:
42484248
value)`` pairs. Order comparisons ('<', '<=', '>=', '>') raise
42494249
:exc:`TypeError`.
42504250

4251+
Dict preserves insertion order. Note that updating key doesn't affects the
4252+
order. On the other hand, keys added after deletion are inserted to the
4253+
last. ::
4254+
4255+
>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4256+
>>> d
4257+
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
4258+
>>> list(d)
4259+
['one', 'two', 'three', 'four']
4260+
>>> list(d.values())
4261+
[1, 2, 3, 4]
4262+
>>> d["one"] = 42
4263+
>>> d
4264+
{'one': 42, 'two': 2, 'three': 3, 'four': 4}
4265+
>>> del d["two"]
4266+
>>> d["two"] = None
4267+
>>> d
4268+
{'one': 42, 'three': 3, 'four': 4, 'two': None}
4269+
4270+
.. versionchanged:: 3.7
4271+
Dict order is guaranteed to be insertion order. This behavior was
4272+
implementation detail of CPython from 3.6.
4273+
42514274
.. seealso::
42524275
:class:`types.MappingProxyType` can be used to create a read-only view
42534276
of a :class:`dict`.

0 commit comments

Comments
 (0)