The sort Method in Python: Sorting a List
Sorts elements of a list. Modifies the original object in place, returning None.
sort(key=None, reverse=False)
key=None-- function that takes an element as an argument, used to extract a comparison key from each element.reverse=False-- flag indicating whether to sort in reverse order.
my_list = [1, 'two', 'a', 4, 'a']
# Attempting to compare incomparable types will raise an exception
my_list.sort() # TypeError: unorderable types: str() <= int()
# Sort so that 'a' is at the end.
my_list.sort(key=lambda val: val == 'a') # Returns None, my_list = [1, 'two', 4, 'a', 'a']
# Effectively, we sorted my_list according to the mask [False, False, False, True, True]