How to sort a list with a lambda expression in Python

Sorting a list with a lambda expression sorts the list with the lambda expression as the key for the sort comparison. For example, sorting [(“Apples”, 5, “20”), (“Pears”, 1, “5”), (“Oranges”, 6, “10”)] with lambda x:x[0] results in [(“Apples”, 5, “20”), (“Oranges”, 6, “10”), (“Pears”, 1, “5”)].

Solution for How to sort a list with a lambda expression in Python : You can use list.sort() to sort with a lambda expression Call list.sort(key=None) with key set to a lambda expression to sort the elements of list by the key.


how-to-sort-a-list-with-a-lambda-expression-in-python