The error is because you are trying to sort a list that contains None
and integer values. This is not possible in Python 3 and is one of its differences with Python 2.
Free translation of the documentation :
The order comparison operators ( <
, <=
, >=
, >
) generate an exception TypeError
when the operands do not have a significant natural order. Therefore, expressions such as 1 < ''
, None > None
or len <= len
are no longer valid, and for example, None < None
generates a TypeError
instead of returning False
. The conclusion is that ordering a heterogeneous list no longer makes sense, all elements must be comparable to each other. Note that this does not apply to operators ==
and !=
. The incomparable objects of different types will always be unequal to each other.
This is possible in Python 2 , it is not possible in Python 3 and, for me, with good reason. An object None
may in our representation of reality may be 0
or ''
or may not. An empty string or 0 is not always "equivalent" to anything. Strictly speaking neither an empty string nor 0 are "nothing".
In Python 2 0 > None
returns True
, which at least makes us think about the meaning of "0", of "nothing", of "infinite", of life, etc ... XD. Out of jokes, this is ambiguous, as is comparing "" > 0
or None > None
.
Your list puntuados
should be something like:
[(1, 2), (3, None), (None, 4)]
sorted
when ordering uses the operator <
and when it is found that it has to evaluate if an integer is or not greater than None
throws the exception according to the above.
It has a solution; or by eliminating the values None
or you change them by an equivalent value that does not change the sense or the result of your code (there are times that can be changed by 0 without problems). You can also do this without altering the original list using the argument key
of sorted
knowing the structure of your list.
However, to be able to give you an option it would be necessary to know how you consider those values you. You must consider if for your algorithm they are equivalent to 0 or they are smaller than any integer. To see how relative this is (and why it is corrected in Python 3), if we understand that None
is less than any integer, how should we consider None
with respect to -∞
?