Newbie: sort list of dictionaries

Raymond Hettinger python at rcn.com
Wed Dec 31 16:27:46 EST 2003


[Sven Brandt]
> > > I have a list of dictionaries. I want the elements of the list to be
> > > sorted by the value of a key of the dict .
> > > 
> > > Excample:
> > > my_list=[{'title': 'foo', 'id': 5, 'text': 'some text'},
> > >           {'title': 'bar', 'id': 3, 'text': 'my text'},
> > >           {'title': 'bla', 'id': 6, 'text': 'any text'}, ]
> > > 
> > > my_list.sort("by the id value")

[Paul Rubin] 
> > The Pythonic way to do it may not be what you're used to, but once you
> > understand it you'll be able to apply the understanding to other areas
> > of programming.
> > 
> > The list sort method lets you pass an optional comparison function,
> > that takes two items and returns -1, 0, or 1, depending on what order
> > the items are supposed to appear in.  The comparison function takes
> > exactly two args (the items to be compared).  You can't pass a third
> > arg to specify the field name.  You could do something ugly like use a
> > global variable, but the preferred way given a field name is to
> > construct a brand new comparison function just for that name:
> > 
> >     def compare_by (fieldname):
> >        def compare_two_dicts (a, b):
> >           return cmp(a[fieldname], b[fieldname])
> >        return compare_two_dicts

[Wade Leftwich] 
> Another way to do it, also Pythonic I think, is with the
> Decorate-Sort-Undecorate (DSU) pattern:
> 
> unsorted_list = [{'id':99, 'title':'a'},{'id':42, 'title':'b'}, ... ] 
> decorated_list = [(x['id'],x) for x in unsorted_list]
> decorated_list.sort()
> sorted_list = [y for (x,y) in decorated_list]
> 
> If performance is a consideration, this is supposed to be much faster
> than providing a comparison function to list.sort().

FWIW, direct support for DSU is built into Py2.4:

>>> from operator import itemgetter
>>> sorted(my_list, key=itemgetter('text'))
[{'text': 'any text', 'id': 6, 'title': 'bla'}, {'text': 'my text',
'id': 3, 'title': 'bar'}, {'text': 'some text', 'id': 5, 'title':
'foo'}]


Raymond Hettinger




More information about the Python-list mailing list