summaryrefslogtreecommitdiffstats
path: root/Doc/whatsnew/3.1.rst
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-02-15 15:44:47 (GMT)
committerGitHub <noreply@github.com>2024-02-15 15:44:47 (GMT)
commitf383ca1a6fa1a2a83c8c1a0e56cf997c77fa2893 (patch)
treeb34725203588d6d143fc9042e6fef829ce7a388e /Doc/whatsnew/3.1.rst
parent9bc32cdc363dfa00293917be3d694df8fa7f7007 (diff)
downloadcpython-f383ca1a6fa1a2a83c8c1a0e56cf997c77fa2893.zip
cpython-f383ca1a6fa1a2a83c8c1a0e56cf997c77fa2893.tar.gz
cpython-f383ca1a6fa1a2a83c8c1a0e56cf997c77fa2893.tar.bz2
[3.12] gh-100734: What's New in 3.x: Add missing detail from 3.x branch (GH-114689) (#115526)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Diffstat (limited to 'Doc/whatsnew/3.1.rst')
-rw-r--r--Doc/whatsnew/3.1.rst22
1 files changed, 22 insertions, 0 deletions
diff --git a/Doc/whatsnew/3.1.rst b/Doc/whatsnew/3.1.rst
index 8cce216..e7c45c9 100644
--- a/Doc/whatsnew/3.1.rst
+++ b/Doc/whatsnew/3.1.rst
@@ -80,6 +80,28 @@ Support was also added for third-party tools like `PyYAML <https://pyyaml.org/>`
PEP written by Armin Ronacher and Raymond Hettinger. Implementation
written by Raymond Hettinger.
+Since an ordered dictionary remembers its insertion order, it can be used
+in conjuction with sorting to make a sorted dictionary::
+
+ >>> # regular unsorted dictionary
+ >>> d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}
+
+ >>> # dictionary sorted by key
+ >>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
+ OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
+
+ >>> # dictionary sorted by value
+ >>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
+ OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])
+
+ >>> # dictionary sorted by length of the key string
+ >>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
+ OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])
+
+The new sorted dictionaries maintain their sort order when entries
+are deleted. But when new keys are added, the keys are appended
+to the end and the sort is not maintained.
+
PEP 378: Format Specifier for Thousands Separator
=================================================