Sorting Keys: for Loops : Dictionary Key « Dictionary « Python






Sorting Keys: for Loops

 

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]

   
  








Related examples in the same category

1.Use variable as a keyUse variable as a key
2.Dictionary: create a new list of keysDictionary: create a new list of keys
3.For loop through the dictionary keysFor loop through the dictionary keys
4.Dictionary: The keys, values, and items FunctionsDictionary: The keys, values, and items Functions
5.Dictionary Keys Are Case-SensitiveDictionary Keys Are Case-Sensitive