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

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

`

497

497

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

``

498

498

`` :meth:extend.

``

499

499

``

500

``

`-

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,

`

501

501

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

`

502

502

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


`503`

`503`

`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

509

509

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

`

510

510

`using a non-existent key.

`

511

511

``

512

``


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

515

515

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

``

516

516

``

517

517

`Here is a small example using a dictionary::

`

518

518

``

519

519

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

`

520

520

` >>> tel['guido'] = 4127

`

521

521

` >>> tel

`

522

``

`-

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

`

``

522

`+

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

`

523

523

` >>> tel['jack']

`

524

524

` 4098

`

525

525

` >>> del tel['sape']

`

526

526

` >>> tel['irv'] = 4127

`

527

527

` >>> 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)

`

532

532

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

`

533

533

` >>> 'guido' in tel

`

534

534

` True

`

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

``

539

539

`key-value pairs::

`

540

540

``

541

541

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

`

542

``

`-

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

`

``

542

`+

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

`

543

543

``

544

544

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

`

545

545

`arbitrary key and value expressions::

`

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

`

551

551

`keyword arguments::

`

552

552

``

553

553

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

`

554

``

`-

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

`

``

554

`+

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

`

555

555

``

556

556

``

557

557

`.. _tut-loopidioms:

`

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

``

710

710

``

711

711

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

`

712

712

``` 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.

`