I have write down a simple code to sort a dictionary,
D1 = {2:3, 1:89, 4:5, 3:0}
sort_dic = {}
print sorted(D1)
for i in sorted(D1):
sort_dic.update({i:D1[i]})
print sort_dic
Which works fine and generates desired output:
{1: 89, 2: 3, 3: 0, 4: 5}
but when, I have changed the dictionary, It won't work and never sorting the dictionary, showing a strange behavior without invoking an error:
D1 = {307: 'LYS', 309: 'ARG', 86: 'GLU', 88: 'VAL', 92: 'ASN', 61: 'ILE', 111: 'ARG'}
sort_dic = {}
print sorted(D1)
for i in sorted(D1):
sort_dic.update({i:D1[i]})
print sort_dic
result with this:
{307: 'LYS', 309: 'ARG', 86: 'GLU', 88: 'VAL', 92: 'ASN', 61: 'ILE', 111: 'ARG'}
thanks for help
Update: Thanks for the code but in this case my main concern Is to understand the distinct behavior of a code with two different dictionaries. Why its happening. Kindly, explain.