Python 2 uses an undocumented ordering, implemented as a .__cmp__()
special method.
The ordering only makes sense in a limited set of use-cases, and only exists because Python 2 tries to make everything orderable.
Python 3 drastically cleaned up Python's ordering story; .__cmp__()
is gone, and only types that actually have a natural ordering (such as numbers and strings) now support ordering. For everything else, you'll need to explicitly define an ordering.
Dictionaries do not have a natural ordering. If you do need to order dictionaries, you need to define an explicit order that makes sense for your use case. If that means comparing just the keys, do so (e.g. use key=sorted
), etc.