bpo-34083: Update dict order in Functional HOWTO (GH-8230)
(cherry picked from commit 5e5bbbec46
)
Co-authored-by: Stig Johan Berggren <stigjb@gmail.com>
This commit is contained in:
parent
6b1b6e4385
commit
151820e7a0
|
@ -273,23 +273,24 @@ dictionary's keys::
|
||||||
|
|
||||||
>>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
|
>>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
|
||||||
... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
|
... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
|
||||||
>>> for key in m: #doctest: +SKIP
|
>>> for key in m:
|
||||||
... print(key, m[key])
|
... print(key, m[key])
|
||||||
Mar 3
|
Jan 1
|
||||||
Feb 2
|
Feb 2
|
||||||
Aug 8
|
Mar 3
|
||||||
Sep 9
|
|
||||||
Apr 4
|
Apr 4
|
||||||
|
May 5
|
||||||
Jun 6
|
Jun 6
|
||||||
Jul 7
|
Jul 7
|
||||||
Jan 1
|
Aug 8
|
||||||
May 5
|
Sep 9
|
||||||
|
Oct 10
|
||||||
Nov 11
|
Nov 11
|
||||||
Dec 12
|
Dec 12
|
||||||
Oct 10
|
|
||||||
|
|
||||||
Note that the order is essentially random, because it's based on the hash
|
Note that starting with Python 3.7, dictionary iteration order is guaranteed
|
||||||
ordering of the objects in the dictionary.
|
to be the same as the insertion order. In earlier versions, the behaviour was
|
||||||
|
unspecified and could vary between implementations.
|
||||||
|
|
||||||
Applying :func:`iter` to a dictionary always loops over the keys, but
|
Applying :func:`iter` to a dictionary always loops over the keys, but
|
||||||
dictionaries have methods that return other iterators. If you want to iterate
|
dictionaries have methods that return other iterators. If you want to iterate
|
||||||
|
@ -301,8 +302,8 @@ The :func:`dict` constructor can accept an iterator that returns a finite stream
|
||||||
of ``(key, value)`` tuples:
|
of ``(key, value)`` tuples:
|
||||||
|
|
||||||
>>> L = [('Italy', 'Rome'), ('France', 'Paris'), ('US', 'Washington DC')]
|
>>> L = [('Italy', 'Rome'), ('France', 'Paris'), ('US', 'Washington DC')]
|
||||||
>>> dict(iter(L)) #doctest: +SKIP
|
>>> dict(iter(L))
|
||||||
{'Italy': 'Rome', 'US': 'Washington DC', 'France': 'Paris'}
|
{'Italy': 'Rome', 'France': 'Paris', 'US': 'Washington DC'}
|
||||||
|
|
||||||
Files also support iteration by calling the :meth:`~io.TextIOBase.readline`
|
Files also support iteration by calling the :meth:`~io.TextIOBase.readline`
|
||||||
method until there are no more lines in the file. This means you can read each
|
method until there are no more lines in the file. This means you can read each
|
||||||
|
|
Loading…
Reference in New Issue