bpo-32337: Update documentats about dict order (GH-4973) · python/cpython@dfbbbf1

@@ -497,7 +497,7 @@ You can't use lists as keys, since lists can be modified in place using index

497497

assignments, slice assignments, or methods like :meth:`append` and

498498

:meth:`extend`.

499499500-

It is best to think of a dictionary as an unordered set of *key: value* pairs,

500+

It is best to think of a dictionary as a set of *key: value* pairs,

501501

with the requirement that the keys are unique (within one dictionary). A pair of

502502

braces creates an empty dictionary: ``{}``. Placing a comma-separated list of

503503

key:value pairs within the braces adds initial key:value pairs to the

@@ -509,26 +509,26 @@ pair with ``del``. If you store using a key that is already in use, the old

509509

value associated with that key is forgotten. It is an error to extract a value

510510

using a non-existent key.

511511512-

Performing ``list(d.keys())`` on a dictionary returns a list of all the keys

513-

used in the dictionary, in arbitrary order (if you want it sorted, just use

514-

``sorted(d.keys())`` instead). [2]_ To check whether a single key is in the

512+

Performing ``list(d)`` on a dictionary returns a list of all the keys

513+

used in the dictionary, in insertion order (if you want it sorted, just use

514+

``sorted(d)`` instead). To check whether a single key is in the

515515

dictionary, use the :keyword:`in` keyword.

516516517517

Here is a small example using a dictionary::

518518519519

>>> tel = {'jack': 4098, 'sape': 4139}

520520

>>> tel['guido'] = 4127

521521

>>> tel

522-

{'sape': 4139, 'guido': 4127, 'jack': 4098}

522+

{'jack': 4098, 'sape': 4139, 'guido': 4127}

523523

>>> tel['jack']

524524

4098

525525

>>> del tel['sape']

526526

>>> tel['irv'] = 4127

527527

>>> tel

528-

{'guido': 4127, 'irv': 4127, 'jack': 4098}

529-

>>> list(tel.keys())

530-

['irv', 'guido', 'jack']

531-

>>> sorted(tel.keys())

528+

{'jack': 4098, 'guido': 4127, 'irv': 4127}

529+

>>> list(tel)

530+

['jack', 'guido', 'irv']

531+

>>> sorted(tel)

532532

['guido', 'irv', 'jack']

533533

>>> 'guido' in tel

534534

True

@@ -539,7 +539,7 @@ The :func:`dict` constructor builds dictionaries directly from sequences of

539539

key-value pairs::

540540541541

>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])

542-

{'sape': 4139, 'jack': 4098, 'guido': 4127}

542+

{'sape': 4139, 'guido': 4127, 'jack': 4098}

543543544544

In addition, dict comprehensions can be used to create dictionaries from

545545

arbitrary key and value expressions::

@@ -551,7 +551,7 @@ When the keys are simple strings, it is sometimes easier to specify pairs using

551551

keyword arguments::

552552553553

>>> dict(sape=4139, guido=4127, jack=4098)

554-

{'sape': 4139, 'jack': 4098, 'guido': 4127}

554+

{'sape': 4139, 'guido': 4127, 'jack': 4098}

555555556556557557

.. _tut-loopidioms:

@@ -710,7 +710,3 @@ interpreter will raise a :exc:`TypeError` exception.

710710711711

.. [1] Other languages may return the mutated object, which allows method

712712

chaining, such as ``d->insert("a")->remove("b")->sort();``.

713-714-

.. [2] Calling ``d.keys()`` will return a :dfn:`dictionary view` object. It

715-

supports operations like membership test and iteration, but its contents

716-

are not independent of the original dictionary -- it is only a *view*.