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.
To access solutions, please obtain an access code from Cambridge University Press at the Lecturer Resources page for my book (registration required) and then sign up to scipython.com providing this code.