@@ -81,6 +81,28 @@ Support was also added for third-party tools like `PyYAML <http://pyyaml.org/>`_
8181 PEP written by Armin Ronacher and Raymond Hettinger. Implementation
8282 written by Raymond Hettinger.
8383
84+ Since an ordered dictionary remembers its insertion order, it can be used
85+ in conjuction with sorting to make a sorted dictionary::
86+
87+ >>> # regular unsorted dictionary
88+ >>> d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}
89+
90+ >>> # dictionary sorted by key
91+ >>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
92+ OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
93+
94+ >>> # dictionary sorted by value
95+ >>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
96+ OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])
97+
98+ >>> # dictionary sorted by length of the key string
99+ >>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
100+ OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])
101+
102+ The new sorted dictionaries maintain their sort order when entries
103+ are deleted. But when new keys are added, the keys are appended
104+ to the end and the sort is not maintained.
105+
84106
85107PEP 378: Format Specifier for Thousands Separator
86108=================================================
0 commit comments