Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort python dictionary keys based on sub-dictionary keys by defining sorting order

My question is an extension to another question where the OP has a dictionary, the one below, and wants to sort main keys based on the sub-dictionary keys

myDict = {
    'SER12346': {'serial_num': 'SER12346', 'site_location': 'North America'},
    'ABC12345': {'serial_num': 'ABC12345', 'site_location': 'South America'},
    'SER12345': {'serial_num': 'SER12345', 'site_location': 'North America'},
    'SER12347': {'serial_num': 'SER12347', 'site_location': 'South America'},
    'ABC12346': {'serial_num': 'ABC12346', 'site_location': 'Europe'}
}

The proposed (quoted below) solution works perfectly.

dicts = myDict.items()
dicts.sort(key=lambda (k,d): (d['site_location'], d['serial_num'],))

However this solution does sorting in ascending order for all (sorting in descending order is straightforward). I was wondering if it is possible to define mix of sorting order, say serial_num in ascending but site_location in descending order?

like image 649
fatih_dur Avatar asked Mar 05 '18 23:03

fatih_dur


People also ask

How do you sort a nested dictionary key in Python?

Method #1 : Using OrderedDict() + sorted() This task can be performed using OrderedDict function which converts the dictionary to specific order as mentioned in its arguments manipulated by sorted function in it to sort by the value of key passed.

Can you sort a dictionary based on keys?

Dictionaries are made up of key: value pairs. Thus, they can be sorted by the keys or by the values.

Can you sort dictionaries by key in Python?

To sort dictionary key in python we can use dict. items() and sorted(iterable) method. Dict. items() method returns an object that stores key-value pairs of dictionaries.


1 Answers

This is one solution, possible because list sorting is stable. I changed the data slightly to demonstrate that it works.

This method is also prescribed in the Python documentation.

myDict = {
    'SER12346': {'serial_num': 'SER12346', 'site_location': 'North America'},
    'ABC12346': {'serial_num': 'ABC12345', 'site_location': 'Europe'},
    'ABC12345': {'serial_num': 'ABC12345', 'site_location': 'South America'},
    'SER12345': {'serial_num': 'SER12345', 'site_location': 'North America'},
    'SER12347': {'serial_num': 'SER12347', 'site_location': 'South America'}
}

result = sorted(myDict.items(), key=lambda x: x[1]['site_location'], reverse=True)
result = sorted(result, key=lambda x: x[1]['serial_num'])

# [('ABC12345', {'serial_num': 'ABC12345', 'site_location': 'South America'}),
#  ('ABC12346', {'serial_num': 'ABC12345', 'site_location': 'Europe'}),
#  ('SER12345', {'serial_num': 'SER12345', 'site_location': 'North America'}),
#  ('SER12346', {'serial_num': 'SER12346', 'site_location': 'North America'}),
#  ('SER12347', {'serial_num': 'SER12347', 'site_location': 'South America'})]
like image 174
jpp Avatar answered Nov 03 '22 23:11

jpp