Custom Sorting Python Dictionary

Dictionaries are inherently unordered, so you can't directly sort the dictionary. You can sort the key/value pairs by sorting someDict.items() and passing in a key function just like you would when sorting anything else, but you will get a sorted list and not a dictionary. See previous questions on dictionary sorting: Python: sorting a dictionary of lists and Dictionary sorting by key length for instance.


You can sort it using OrderedDict and by specifying your Custom-order.

def customsort(dict1 , key_order):
    items = [dict1[k] if k in dict1.keys() else 0 for k in key_order] 
    sorted_dict = OrderedDict()
    for i in range(len(key_order)):
        sorted_dict[key_order[i]] = items[i]
    return sorted_dict
key_order = [ "monday" ,"tuesday" ,"wednesday" ,"thursday" ,"friday" ,"saturday"]
dict1 ={"monday" : 10 , "thursday" :12 , "wednesday" : 34}
sorted_dicti = customsort(dict1,key_order)
print(sorted_dicti)

customsort() sorts given dictionary(dict1) by order(key_order) passed by user.

items = [dict1[k] if k in dict1.keys() else 0 for k in key_order] 

It will check if the given key is in dict1 if it is there then put value specified in dict1 else put value as 0.

OrderedDict([('monday', 10), ('tuesday', 0), ('wednesday', 34), ('thursday', 12), ('friday', 0), ('saturday', 0)])

Updated answer for Python 3.6+

>>> d = {'10': -10, 'ZT21': 14, 'WX21': 12, '2': 15, '5': -3, 'UM': -25}
>>> keyorder = ['ZT21', '10', 'WX21', 'UM', '5', '2']
>>> {k: d[k] for k in keyorder if k in d}
{'ZT21': 14, '10': -10, 'WX21': 12, 'UM': -25, '5': -3, '2': 15}

Legacy answer: Dictionaries in Python are unordered (before Python3.6). You can get the results you need as a list

>>> d = {'10': -10, 'ZT21': 14, 'WX21': 12, '2': 15, '5': -3, 'UM': -25}
>>> keyorder = ['ZT21', '10', 'WX21', 'UM', '5', '2']
>>> sorted(d.items(), key=lambda i:keyorder.index(i[0]))
[('ZT21', 14), ('10', -10), ('WX21', 12), ('UM', -25), ('5', -3), ('2', 15)]

or as an OrderedDict

>>> from collections import OrderedDict
>>> OrderedDict(sorted(d.items(), key=lambda i:keyorder.index(i[0])))
OrderedDict([('ZT21', 14), ('10', -10), ('WX21', 12), ('UM', -25), ('5', -3), ('2', 15)])

If you are doing a lot of these, it will be more efficient to use a dict for the keyorder

>>> keyorder = {k:v for v,k in enumerate(['ZT21', '10', 'WX21', 'UM', '5', '2'])}
>>> OrderedDict(sorted(d.items(), key=lambda i:keyorder.get(i[0])))
OrderedDict([('ZT21', 14), ('10', -10), ('WX21', 12), ('UM', -25), ('5', -3), ('2', 15)])

You can't. Use a collections.OrderedDict instead.

Tags:

Python