May 2017
Intermediate to advanced
310 pages
8h 5m
English
If we want to do a simple sort on either the keys or values of a dictionary, we can do the following:

Note that the first line in the preceding code sorts the keys according to alphabetical order, and the second line sorts the values in order of integer value.
The sorted() method has two optional arguments that are of interest: key and reverse. The key argument has nothing to do with the dictionary keys, but rather is a way of passing a function to the sort algorithm to determine the sort order. For example, in the following code, we use the __getitem__ special method to sort the dictionary keys according to the dictionary ...