bpo-33609: Document dict insertion order guarantee as of 3.7 (GH-7093) · python/cpython@8585ec9 (original) (raw)
``` @@ -4229,6 +4229,29 @@ pairs within braces, for example: {'jack': 4098, 'sjoerd': 4127}
or ``{4098:
`4229`
`4229`
``` value)`` pairs. Order comparisons ('<', '<=', '>=', '>') raise
4230
4230
`` :exc:TypeError
.
``
4231
4231
``
``
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
+
4232
4255
`.. seealso::
`
4233
4256
`` :class:types.MappingProxyType
can be used to create a read-only view
``
4234
4257
`` of a :class:dict
.
``