''' 20210930 Robin Dawes Using lamda functions to sort things ''' # a list of dimensions of some rectangles list_a = [ [3, 7], [9, 5], [1, 8], [4, 12], [18, 2] ] print("Sorting rectangles by area") print ("Before sorting:",list_a) list_a.sort(key = lambda x : x[0]*x[1]) #sort by the area of the rectangles # the lambda function here is equivalent to # def (x): # return x[0]*x[1] print("\nAfter sorting:", list_a) print("\n\nSorting rectangles by largest dimension") # using the built-in function "max" to determine the key for sorting list_a.sort(key = lambda x : max(x)) print("\nAfter sorting:", list_a) def avg(a, b): return (a+b)/2 print("\n\nSorting rectangles by average of dimensions") # using a locally defined function to determine the key value list_a.sort(key = lambda x : avg(x[0],x[1])) print("\nAfter sorting:", list_a)