List methods

Lists have only one special method: list.sort(*, key=None, reverse=False). By default, sort() performs an A-Z-style sort, with lower values on the left. Using key allows the use of an additional function to modify the default sort, while reverse performs a Z-A sort. An example of these sorting operations is shown in the following screenshot:

List sorting methods

In line 145, a list of strings is created and the default sort is performed in line 146. The results of the default sort are shown in line 147. Note that capital letters come before lowercase ones.

In line 148, the key modifier is used to take lowercase letters into account, resulting in the unusual results of line 149. These are unusual because, rather than simply moving the lowercase words to the beginning of the list, the sort() method has actually sorted the list according to the actual alphabetical listing of the beginning characters, regardless of their capitalization.

Line 150 performs a reverse sort. The output in line 151 shows that capitalization doesn't matter, as we didn't specify a key, so the results are simply the opposite of line 147.