Sorting a dictionary

Question Q4.3.4

Dictionaries are unsorted data structures. Write a one-line Python statement returning a list of (key, value) pairs sorted by key. Assume that all keys have the same data type (why is this important?) Repeat the exercise to produce a list ordered by dictionary values.


Solution

To access solutions, please obtain an access code from Cambridge University Press at the Lecturer Resources page for my book (registration required) and then sign up to scipython.com providing this code.