I have a dictionary that looks like this:
myDict = {
\'SER12346\': {\'serial_num\': \'SER12346\', \'site_location\': \'North America\'},
\'ABC12345\'
Is this what you wanted?
dicts = [{k: v} for (k,v) in myDict.items()]
dicts.sort(key=lambda d: (d.values()[0]['site_location'], d.values()[0]['serial_num'],))
Output for doing:
import pprint
pprint.pprint(dicts)
is:
[{'ABC12346': {'serial_num': 'ABC12346', 'site_location': 'Europe'}},
{'SER12345': {'serial_num': 'SER12345', 'site_location': 'North America'}},
{'SER12346': {'serial_num': 'SER12346', 'site_location': 'North America'}},
{'ABC12345': {'serial_num': 'ABC12345', 'site_location': 'South America'}},
{'SER12347': {'serial_num': 'SER12347', 'site_location': 'South America'}}]
EDIT: I was going on your answer for the output format, but this would probably make more sense:
dicts = myDict.items()
dicts.sort(key=lambda (k,d): (d['site_location'], d['serial_num'],))
Output:
[('ABC12346', {'serial_num': 'ABC12346', 'site_location': 'Europe'}),
('SER12345', {'serial_num': 'SER12345', 'site_location': 'North America'}),
('SER12346', {'serial_num': 'SER12346', 'site_location': 'North America'}),
('ABC12345', {'serial_num': 'ABC12345', 'site_location': 'South America'}),
('SER12347', {'serial_num': 'SER12347', 'site_location': 'South America'})]