
Daniel U. answered 04/30/19
Tutor
5
(44)
Professional Software Developer and PhD Here To Help
# This isn't really something people typically do, since
# you can simply sort the keys and then access the dictionary
# as if it's sorted, but here's an example of creating a dict
# y that is the sorted version of the dict x.
x={2:3, 1:89, 4:5, 3:0}
y={}
for key in sorted([key for key in x]):
y[key]=x[key]
print(x) # {2: 3, 1: 89, 4: 5, 3: 0}
print(y) # {1: 89, 2: 3, 3: 0, 4: 5}