Because list.sort() sorts a list in place, it cannot be used in in a in a for e in L.sort(): … statement. Such a for loop is possible, however, with for e in sorted(L): ….
Sorting a list based on a characteristic of its elements
With the key parameter of list.sort and a lambda expression, it is possible to sort the elements of a list based on an arbitrary characteristic of its element.
The following example sorts the words in a list in order of the length of the words: