Skip to content

Commit 6beec6c

Browse files
miss-islingtonYzi-Literryjreedy
authored
[3.13] gh-133361: move the explanation of dict equal before its use (GH-133424) (#133621)
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 ce9d708 commit 6beec6c

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
@@ -4629,7 +4629,13 @@ can be used interchangeably to index the same dictionary entry.
46294629
being added is already present, the value from the keyword argument
46304630
replaces the value from the positional argument.
46314631

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

46354641
>>> a = dict(one=1, two=2, three=3)
@@ -4644,6 +4650,27 @@ can be used interchangeably to index the same dictionary entry.
46444650
Providing keyword arguments as in the first example only works for keys that
46454651
are valid Python identifiers. Otherwise, any valid keys can be used.
46464652

4653+
Dictionaries preserve insertion order. Note that updating a key does not
4654+
affect the order. Keys added after deletion are inserted at the end. ::
4655+
4656+
>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4657+
>>> d
4658+
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
4659+
>>> list(d)
4660+
['one', 'two', 'three', 'four']
4661+
>>> list(d.values())
4662+
[1, 2, 3, 4]
4663+
>>> d["one"] = 42
4664+
>>> d
4665+
{'one': 42, 'two': 2, 'three': 3, 'four': 4}
4666+
>>> del d["two"]
4667+
>>> d["two"] = None
4668+
>>> d
4669+
{'one': 42, 'three': 3, 'four': 4, 'two': None}
4670+
4671+
.. versionchanged:: 3.7
4672+
Dictionary order is guaranteed to be insertion order. This behavior was
4673+
an implementation detail of CPython from 3.6.
46474674

46484675
These are the operations that dictionaries support (and therefore, custom
46494676
mapping types should support too):
@@ -4814,32 +4841,6 @@ can be used interchangeably to index the same dictionary entry.
48144841

48154842
.. versionadded:: 3.9
48164843

4817-
Dictionaries compare equal if and only if they have the same ``(key,
4818-
value)`` pairs (regardless of ordering). Order comparisons ('<', '<=', '>=', '>') raise
4819-
:exc:`TypeError`.
4820-
4821-
Dictionaries preserve insertion order. Note that updating a key does not
4822-
affect the order. Keys added after deletion are inserted at the end. ::
4823-
4824-
>>> d = {"one": 1, "two": 2, "three": 3, "four": 4}
4825-
>>> d
4826-
{'one': 1, 'two': 2, 'three': 3, 'four': 4}
4827-
>>> list(d)
4828-
['one', 'two', 'three', 'four']
4829-
>>> list(d.values())
4830-
[1, 2, 3, 4]
4831-
>>> d["one"] = 42
4832-
>>> d
4833-
{'one': 42, 'two': 2, 'three': 3, 'four': 4}
4834-
>>> del d["two"]
4835-
>>> d["two"] = None
4836-
>>> d
4837-
{'one': 42, 'three': 3, 'four': 4, 'two': None}
4838-
4839-
.. versionchanged:: 3.7
4840-
Dictionary order is guaranteed to be insertion order. This behavior was
4841-
an implementation detail of CPython from 3.6.
4842-
48434844
Dictionaries and dictionary views are reversible. ::
48444845

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

0 commit comments

Comments
 (0)