Given the following dict
:
class Object:
def __repr__(self): return "Object"
foo = {
1: [(10, Object()), (10, Object())],
2: [(11, Object()), (10, Object())]
}
sorted(foo.items(), key= lambda x : x[1][0], reverse=True)
Using sorted function, yields, as expected
[
(2, [(11, Object), (10, Object)]),
(1, [(10, Object), (10, Object)])
]
The problem arises when the first term is the same for all values in the dict
.
foo2 = {
1: [(10, Object()), (10, Object())],
2: [(10, Object()), (10, Object())]
}
Since __lt__()
isn't implemented in Object
, sorted()
throws a TypeError
exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'Object' and 'Object'
In this case sorting would not be necessary but the algorithm I'm using needs to do it anyway.
What could I do to avoid this error?