Sort list of dictionaries by key (case insensitive)

Chris Rebert clp2 at rebertia.com
Wed Jan 13 05:41:25 EST 2010


On Tue, Jan 12, 2010 at 11:45 PM, Nico Grubert <nicogrubert at gmail.com> wrote:
> Hi there
>
> I have the following list 'mylist' that contains some dictionaries:
>
> mylist = [{'title':'the Fog', 'id':1},
>          {'title':'The Storm', 'id':2},
>          {'title':'the bible', 'id':3},
>          {'title':'The thunder', 'id':4}
>         ]

mylist.sort(key = lambda d: d['title'])

Use operator.itemgetter() to optimize in the unlikely event it becomes
necessary.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list