Regular Python dictionaries iterate over key/value pairs in arbitrary order. Over the years, a number of authors have written alternative implementations that remember the order that the keys were originally inserted. Based on the experiences from those implementations, 2.7 introduces a new
OrderedDict
class in the collections
module.
The
OrderedDict
API provides the same interface as regular dictionaries but iterates over keys and values in a guaranteed order depending on when a key was first inserted:
If a new entry overwrites an existing entry, the original insertion position is left unchanged:
Deleting an entry and reinserting it will move it to the end:
The
popitem()
method has an optional last argument that defaults to True
. If last is true, the most recently added key is returned and removed; if it’s false, the oldest key is selected:
Comparing two ordered dictionaries checks both the keys and values, and requires that the insertion order was the same:
Comparing an
OrderedDict
with a regular dictionary ignores the insertion order and just compares the keys and values.
How does the
OrderedDict
work? It maintains a doubly-linked list of keys, appending new keys to the list as they’re inserted. A secondary dictionary maps keys to their corresponding list node, so deletion doesn’t have to traverse the entire linked list and therefore remains O(1).
The standard library now supports use of ordered dictionaries in several modules.
- The
ConfigParser
module uses them by default, meaning that configuration files can now be read, modified, and then written back in their original order. - The
_asdict()
method forcollections.namedtuple()
now returns an ordered dictionary with the values appearing in the same order as the underlying tuple indices. - The
json
module’sJSONDecoder
class constructor was extended with an object_pairs_hook parameter to allowOrderedDict
instances to be built by the decoder. Support was also added for third-party tools like PyYAML.
See also
- PEP 372 - Adding an ordered dictionary to collections
- PEP written by Armin Ronacher and Raymond Hettinger; implemented by Raymond Hettinger.
No comments:
Post a Comment