Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
55 changes: 28 additions & 27 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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}
Expand Down
Loading