mirror of
https://github.com/python/cpython.git
synced 2025-07-30 06:34:15 +00:00
bpo-32337: Update documentats about dict order (GH-4973)
This commit is contained in:
parent
c869529ea9
commit
dfbbbf16f9
4 changed files with 21 additions and 23 deletions
|
@ -4256,17 +4256,17 @@ support membership tests:
|
|||
Return an iterator over the keys, values or items (represented as tuples of
|
||||
``(key, value)``) in the dictionary.
|
||||
|
||||
Keys and values are iterated over in an arbitrary order which is non-random,
|
||||
varies across Python implementations, and depends on the dictionary's history
|
||||
of insertions and deletions. If keys, values and items views are iterated
|
||||
over with no intervening modifications to the dictionary, the order of items
|
||||
will directly correspond. This allows the creation of ``(value, key)`` pairs
|
||||
Keys and values are iterated over in insertion order.
|
||||
This allows the creation of ``(value, key)`` pairs
|
||||
using :func:`zip`: ``pairs = zip(d.values(), d.keys())``. Another way to
|
||||
create the same list is ``pairs = [(v, k) for (k, v) in d.items()]``.
|
||||
|
||||
Iterating views while adding or deleting entries in the dictionary may raise
|
||||
a :exc:`RuntimeError` or fail to iterate over all entries.
|
||||
|
||||
.. versionchanged:: 3.7
|
||||
Dict order is guaranteed to be insertion order.
|
||||
|
||||
.. describe:: x in dictview
|
||||
|
||||
Return ``True`` if *x* is in the underlying dictionary's keys, values or
|
||||
|
@ -4293,9 +4293,9 @@ An example of dictionary view usage::
|
|||
>>> print(n)
|
||||
504
|
||||
|
||||
>>> # keys and values are iterated over in the same order
|
||||
>>> # keys and values are iterated over in the same order (insertion order)
|
||||
>>> list(keys)
|
||||
['eggs', 'bacon', 'sausage', 'spam']
|
||||
['eggs', 'sausage', 'bacon', 'spam']
|
||||
>>> list(values)
|
||||
[2, 1, 1, 500]
|
||||
|
||||
|
@ -4303,7 +4303,7 @@ An example of dictionary view usage::
|
|||
>>> del dishes['eggs']
|
||||
>>> del dishes['sausage']
|
||||
>>> list(keys)
|
||||
['spam', 'bacon']
|
||||
['bacon', 'spam']
|
||||
|
||||
>>> # set operations
|
||||
>>> keys & {'eggs', 'bacon', 'salad'}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue