Python Data Structures
Dictionaries
- Python dictionaries don't have a single __repr__ value - it depends on the order of the items.
- A non-nested dictionary d can be converted into a form suitable for use as a key using frozenset(d.items()). For more generality, use the code here.
- OrderedDicts were added to the standard library in 3.1 and 2.7. According to the documentation, for Python versions 2.4 or later this code should be used. There is also some code from Raymond Hettinger, one of the contributors to the PEP. The code here is claimed to work under 2.6 and 3.0 and was made for the proposal. Answer by Casebash
Sets
- Creating a custom set: The obvious option is to override Collections.set, but this can require a lot of work to override every method. This recipe will allow you to provide a custom equality/uniqueness function. If you want more control, overriding the methods in collections.MutableSet, will provide most methods for free, with the notable exception of update. In older version of Python, the abstract base class collections.Set (which is different from set) was used instead.
page revision: 10, last edited: 06 Mar 2010 05:16