Sub-sort after sort

Paul Hankin paul.hankin at gmail.com
Fri Nov 2 20:05:23 EDT 2007


On Nov 2, 9:45 pm, kee... at gmail.com wrote:
> Hello, I have been sorting a list of dicts using the following
> function:
>
> result_rs = sorted(unsort_rs, key=itemgetter(orderby))
>
> and this works fine. Now I am looking to perform a subsort as well.
> For example, I have this:
>
> test = [{'name': 'John Smith', 'location': 'CA',},{'name': 'John
> Smith', 'location': 'AZ',},]
>
> I would want to sort by name first, then sub sort by location. Any
> ideas? Thanks!

test.sort(key=lambda x:(x['name'], x['location']))

--
Paul Hankin




More information about the Python-list mailing list