[Tutor] Sorting and secondary sorting.

Liam Clarke ml.cyresse at gmail.com
Sat Mar 11 14:53:45 CET 2006

Hi all,

I'm trying to think of a way to sort a list of dictionaries. In pseudo-code:

l = [ { "host":"foo", "db":"bob"},
       { "host":"foo", "db":"dave"},
       { "host":"fee", "db":"henry"}

l.sort( key = lambda item: item["host"], second_key = lambda item: item["db"])

Which, if all went well, would give me -

l = [ { "host":"fee", "db":"henry"}
       { "host":"foo", "db":"bob"},
       { "host":"foo", "db":"dave"},

So, I'm trying to sort and then do a secondary sort. I'd like to do it
Pythonically; currently I'm creating a Pysqlite db in memory and
sticking the data in a table, and selecting it back out with an ORDER
BY clause, and then reconstituting it into dictionaries in a list.

I get that "overlooking something simple" feeling on this one, so any
assistance welcomed.
I got lost with lists and lists of lists and joining lists back
together, so I cheated and went the SQL way.


Liam Clarke

More information about the Tutor mailing list