All the examples that I could find ( in the documentation , etc.) define OrderedDicts, passing the data to the constructor. From the docs:
# regular unsorted dictionary d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}
On the other hand, you can initialize an OrderedDict without providing any parameters to the constructors, which preserves the order in which key pairs and values ββare added.
I am looking for some kind of construct that looks like the following, except without "d.items ()". In fact, I ask him to remember the mechanism without giving him an example that might seem crazy. Is my only option to βhackβ this by providing the original βdβ (below) with one element, or is there a better way?
OrderedDict(sorted(d.items(), key=lambda t: t[0]))
Thanks!
source share