gh-133361: move the explanation of dict equal before its use (#133424)

Also move up the explanation of insertion order preservation.  Both paragraphs seemed out of place down where they were.
---------

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
This commit is contained in:
Yongzi Li 2025-05-08 06:04:49 +08:00 committed by GitHub
parent 79b8a32fcb
commit 61ac88c06e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4823,7 +4823,13 @@ can be used interchangeably to index the same dictionary entry.
being added is already present, the value from the keyword argument
replaces the value from the positional argument.
To illustrate, the following examples all return a dictionary equal to
Providing keyword arguments as in the first example only works for keys that
are valid Python identifiers. Otherwise, any valid keys can be used.
Dictionaries compare equal if and only if they have the same ``(key,
value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise
:exc:`TypeError`. To illustrate dictionary creation and equality,
the following examples all return a dictionary equal to
``{"one": 1, "two": 2, "three": 3}``::
>>> a = dict(one=1, two=2, three=3)
@ -4838,6 +4844,27 @@ can be used interchangeably to index the same dictionary entry.
Providing keyword arguments as in the first example only works for keys that
are valid Python identifiers. Otherwise, any valid keys can be used.
Dictionaries preserve insertion order. Note that updating a key does not
affect the order. Keys added after deletion are inserted at the end. ::
>>> 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
Dictionary order is guaranteed to be insertion order. This behavior was
an implementation detail of CPython from 3.6.
These are the operations that dictionaries support (and therefore, custom
mapping types should support too):
@ -5008,32 +5035,6 @@ can be used interchangeably to index the same dictionary entry.
.. versionadded:: 3.9
Dictionaries compare equal if and only if they have the same ``(key,
value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise
:exc:`TypeError`.
Dictionaries preserve insertion order. Note that updating a key does not
affect the order. Keys added after deletion are inserted at the end. ::
>>> 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
Dictionary order is guaranteed to be insertion order. This behavior was
an implementation detail of CPython from 3.6.
Dictionaries and dictionary views are reversible. ::
>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}