bpo-34083: Update dict order in Functional HOWTO (GH-8230) · python/cpython@5e5bbbe (original) (raw)

`@@ -273,23 +273,24 @@ dictionary's keys::

`

273

273

``

274

274

` >>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,

`

275

275

` ... 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}

`

276

``

`-

for key in m: #doctest: +SKIP

`

``

276

`+

for key in m:

`

277

277

` ... print(key, m[key])

`

278

``

`-

Mar 3

`

``

278

`+

Jan 1

`

279

279

` Feb 2

`

280

``

`-

Aug 8

`

281

``

`-

Sep 9

`

``

280

`+

Mar 3

`

282

281

` Apr 4

`

``

282

`+

May 5

`

283

283

` Jun 6

`

284

284

` Jul 7

`

285

``

`-

Jan 1

`

286

``

`-

May 5

`

``

285

`+

Aug 8

`

``

286

`+

Sep 9

`

``

287

`+

Oct 10

`

287

288

` Nov 11

`

288

289

` Dec 12

`

289

``

`-

Oct 10

`

290

290

``

291

``

`-

Note that the order is essentially random, because it's based on the hash

`

292

``

`-

ordering of the objects in the dictionary.

`

``

291

`+

Note that starting with Python 3.7, dictionary iteration order is guaranteed

`

``

292

`+

to be the same as the insertion order. In earlier versions, the behaviour was

`

``

293

`+

unspecified and could vary between implementations.

`

293

294

``

294

295

`` Applying :func:iter to a dictionary always loops over the keys, but

``

295

296

`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

``

301

302

``` of (key, value) tuples:

```

302

303

``

303

304

` >>> L = [('Italy', 'Rome'), ('France', 'Paris'), ('US', 'Washington DC')]

`

304

``

`-

dict(iter(L)) #doctest: +SKIP

`

305

``

`-

{'Italy': 'Rome', 'US': 'Washington DC', 'France': 'Paris'}

`

``

305

`+

dict(iter(L))

`

``

306

`+

{'Italy': 'Rome', 'France': 'Paris', 'US': 'Washington DC'}

`

306

307

``

307

308

`` Files also support iteration by calling the :meth:~io.TextIOBase.readline

``

308

309

`method until there are no more lines in the file. This means you can read each

`