Python Principles
Python Q&A

How do I sort a dictionary?


You can't, but you can create a representation of the dictionary as a list, and then sort that list with the built-in function sorted(). Here is an example:

d = {3: 4, 1: 2}
sorted_list = sorted(d.items())
for key, value in sorted_list:
    print(key, value)

Running the code prints:

1 2
3 4