Learning Scientific Programming with Python (2nd edition)
Q4.3.6: Sorting a list containing None
Question Q4.3.6
The built-in function sorted
and sequence method sort
require that the elements in the sequence be of types that can be compared: they will fail, for example, if a list contains a mixture of strings and numbers. However, it is frequently the case that a list contains numbers and the special value, None
(perhaps denoting missing data). Devise a way to sort such a list by passing a lambda
function in the argument key
; the None
values should end up at the end of the sorted list.