Skip to content

Commit cc588aa

Browse files
miss-islingtonYzi-Literryjreedy
authored
[3.14] gh-133361: move the explanation of dict equal before its use (GH-133424) (#133620)
gh-133361: move the explanation of dict equal before its use (GH-133424) Also move up the explanation of insertion order preservation. Both paragraphs seemed out of place down where they were. --------- (cherry picked from commit 61ac88c) Co-authored-by: Yongzi Li <[email protected]> Co-authored-by: Terry Jan Reedy <[email protected]>
1 parent e2d440e commit cc588aa

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

Doc/library/stdtypes.rst

+28-27
Original file line numberDiff line numberDiff line change
@@ -4823,7 +4823,13 @@ can be used interchangeably to index the same dictionary entry.
48234823
being added is already present, the value from the keyword argument
48244824
replaces the value from the positional argument.
48254825

4826-
To illustrate, the following examples all return a dictionary equal to
4826+
Providing keyword arguments as in the first example only works for keys that
4827+
are valid Python identifiers. Otherwise, any valid keys can be used.
4828+
4829+
Dictionaries compare equal if and only if they have the same ``(key,
4830+
value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise
4831+
:exc:`TypeError`. To illustrate dictionary creation and equality,
4832+
the following examples all return a dictionary equal to
48274833
``{"one": 1, "two": 2, "three": 3}``::
48284834

48294835
>>> a = dict(one=1, two=2, three=3)
@@ -4838,6 +4844,27 @@ can be used interchangeably to index the same dictionary entry.
48384844
Providing keyword arguments as in the first example only works for keys that
48394845
are valid Python identifiers. Otherwise, any valid keys can be used.
48404846

4847+
Dictionaries preserve insertion order. Note that updating a key does not
4848+
affect the order. Keys added after deletion are inserted at the end. ::
4849+
4850+
>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4851+
>>> d
4852+
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
4853+
>>> list(d)
4854+
['one', 'two', 'three', 'four']
4855+
>>> list(d.values())
4856+
[1, 2, 3, 4]
4857+
>>> d["one"] = 42
4858+
>>> d
4859+
{'one': 42, 'two': 2, 'three': 3, 'four': 4}
4860+
>>> del d["two"]
4861+
>>> d["two"] = None
4862+
>>> d
4863+
{'one': 42, 'three': 3, 'four': 4, 'two': None}
4864+
4865+
.. versionchanged:: 3.7
4866+
Dictionary order is guaranteed to be insertion order. This behavior was
4867+
an implementation detail of CPython from 3.6.
48414868

48424869
These are the operations that dictionaries support (and therefore, custom
48434870
mapping types should support too):
@@ -5008,32 +5035,6 @@ can be used interchangeably to index the same dictionary entry.
50085035

50095036
.. versionadded:: 3.9
50105037

5011-
Dictionaries compare equal if and only if they have the same ``(key,
5012-
value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise
5013-
:exc:`TypeError`.
5014-
5015-
Dictionaries preserve insertion order. Note that updating a key does not
5016-
affect the order. Keys added after deletion are inserted at the end. ::
5017-
5018-
>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
5019-
>>> d
5020-
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
5021-
>>> list(d)
5022-
['one', 'two', 'three', 'four']
5023-
>>> list(d.values())
5024-
[1, 2, 3, 4]
5025-
>>> d["one"] = 42
5026-
>>> d
5027-
{'one': 42, 'two': 2, 'three': 3, 'four': 4}
5028-
>>> del d["two"]
5029-
>>> d["two"] = None
5030-
>>> d
5031-
{'one': 42, 'three': 3, 'four': 4, 'two': None}
5032-
5033-
.. versionchanged:: 3.7
5034-
Dictionary order is guaranteed to be insertion order. This behavior was
5035-
an implementation detail of CPython from 3.6.
5036-
50375038
Dictionaries and dictionary views are reversible. ::
50385039

50395040
>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}

0 commit comments

Comments
 (0)