Skip to content

Commit 9216dff

Browse files
bpo-32337: Update documentats about dict order (GH-4973)
(cherry picked from commit dfbbbf1) Co-authored-by: hui shang <[email protected]>
1 parent 74735e2 commit 9216dff

File tree

4 files changed

+21
-23
lines changed

4 files changed

+21
-23
lines changed

Doc/library/stdtypes.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4256,17 +4256,17 @@ support membership tests:
42564256
Return an iterator over the keys, values or items (represented as tuples of
42574257
``(key, value)``) in the dictionary.
42584258

4259-
Keys and values are iterated over in an arbitrary order which is non-random,
4260-
varies across Python implementations, and depends on the dictionary's history
4261-
of insertions and deletions. If keys, values and items views are iterated
4262-
over with no intervening modifications to the dictionary, the order of items
4263-
will directly correspond. This allows the creation of ``(value, key)`` pairs
4259+
Keys and values are iterated over in insertion order.
4260+
This allows the creation of ``(value, key)`` pairs
42644261
using :func:`zip`: ``pairs = zip(d.values(), d.keys())``. Another way to
42654262
create the same list is ``pairs = [(v, k) for (k, v) in d.items()]``.
42664263

42674264
Iterating views while adding or deleting entries in the dictionary may raise
42684265
a :exc:`RuntimeError` or fail to iterate over all entries.
42694266

4267+
.. versionchanged:: 3.7
4268+
Dict order is guaranteed to be insertion order.
4269+
42704270
.. describe:: x in dictview
42714271

42724272
Return ``True`` if *x* is in the underlying dictionary's keys, values or
@@ -4293,17 +4293,17 @@ An example of dictionary view usage::
42934293
>>> print(n)
42944294
504
42954295

4296-
>>> # keys and values are iterated over in the same order
4296+
>>> # keys and values are iterated over in the same order (insertion order)
42974297
>>> list(keys)
4298-
['eggs', 'bacon', 'sausage', 'spam']
4298+
['eggs', 'sausage', 'bacon', 'spam']
42994299
>>> list(values)
43004300
[2, 1, 1, 500]
43014301

43024302
>>> # view objects are dynamic and reflect dict changes
43034303
>>> del dishes['eggs']
43044304
>>> del dishes['sausage']
43054305
>>> list(keys)
4306-
['spam', 'bacon']
4306+
['bacon', 'spam']
43074307

43084308
>>> # set operations
43094309
>>> keys & {'eggs', 'bacon', 'salad'}

Doc/tutorial/datastructures.rst

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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`.
499499

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,
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.
511511

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
515515
dictionary, use the :keyword:`in` keyword.
516516

517517
Here is a small example using a dictionary::
518518

519519
>>> 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::
540540

541541
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
542-
{'sape': 4139, 'jack': 4098, 'guido': 4127}
542+
{'sape': 4139, 'guido': 4127, 'jack': 4098}
543543

544544
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::
552552

553553
>>> dict(sape=4139, guido=4127, jack=4098)
554-
{'sape': 4139, 'jack': 4098, 'guido': 4127}
554+
{'sape': 4139, 'guido': 4127, 'jack': 4098}
555555

556556

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

711711
.. [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*.

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,7 @@ Ian Seyer
14491449
Dmitry Shachnev
14501450
Anish Shah
14511451
Daniel Shahaf
1452+
Hui Shang
14521453
Mark Shannon
14531454
Ha Shao
14541455
Richard Shapiro
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update documentation related with ``dict`` order.

0 commit comments

Comments
 (0)