Oct-16-2020, 03:34 PM
A lambda is a just a function without a name (hence "anonymous"). You can certainly use a named function if it helps you understand it better.
def second_element(s): """Return the second element of the string after split()ing""" """Will raise IndexError if there are not two elements""" return s.split()[1] def sort_by_surname_desc(names): return sorted(names, key=second_element, reverse=True)The benefit of lambda is that if the function is super short, you don't have to define it elsewhere. You can just define it where it's used.