Mega Code Archive

 
Categories / Python / Dictionary
 

Sorting Keys

D = {'a': 1, 'b': 2, 'c': 3} Ks = D.keys(  )                         # Unordered keys list print Ks  Ks.sort(  )                             # Sorted keys list print Ks  for key in Ks:                       # Iterate though sorted keys     print key, '=>', D[key]  for key in sorted(D):     print key, '=>', D[key]