I am studying for my GCSE part of which requires me to print a dictionary sorted alphabetically by key and the print should include the associated value.
I have spen
>>> class1 = { 'Ethan':'9','Ian':'3','Helen':'8','Holly':'6' }
>>> print(sorted(class1.items()))
[('Ethan', '9'), ('Helen', '8'), ('Holly', '6'), ('Ian', '3')]
>>> for k,v in sorted(class1.items()):
... print(k, v)
...
Ethan 9
Helen 8
Holly 6
Ian 3
>>> for k,v in sorted(class1.items(), key=lambda p:p[1]):
... print(k,v)
...
Ian 3
Holly 6
Helen 8
Ethan 9
>>> for k,v in sorted(class1.items(), key=lambda p:p[1], reverse=True):
... print(k,v)
...
Ethan 9
Helen 8
Holly 6
Ian 3