Sorting a dictionary

Question Q4.3.4

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


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 providing this code.