In Python 2 you can compare dictionaries with the less-than and greater-than operators, meaning that they can be sorted/ordered:
>>> {'a': 2} < {'a': 3}
True
>>> {'a': 7} < {'a': 3}
False
>>> {'b': 4} > {'c': 1}
False
>>> sorted([{'c': 1}, {'a': 5}, {'b':2 }])
[{'a': 5}, {'b': 2}, {'c': 1}]
In Python 3 you cannot do that:
>>> {'a': 2} < {'a': 3}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'dict' and 'dict'
>>> sorted([{'c': 1}, {'a': 5}, {'b':2 }])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'dict' and 'dict'
- How are the dictionaries compared in Python 2? (how is the
<
-operator defined?) - And when was this change introduced? (which specific version? And why?)