Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Sort list of dictionaries by key (case insensitive)

Reply
Thread Tools

Sort list of dictionaries by key (case insensitive)

 
 
Nico Grubert
Guest
Posts: n/a
 
      01-13-2010
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}
]

How I can sort (case insensitive) the list by the dictioary's 'title' key?

The result should be this list:
[{'title':'the bible', 'id':3},
{'title':'the Fog', 'id':1},
{'title':'The Storm', 'id':2},
{'title':'The thunder', 'id':4}
]

I am using Python 2.4.


Regards,
Nico
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-13-2010
Nico Grubert wrote:

> 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}
> ]
>
> How I can sort (case insensitive) the list by the dictioary's 'title' key?
>
> The result should be this list:
> [{'title':'the bible', 'id':3},
> {'title':'the Fog', 'id':1},
> {'title':'The Storm', 'id':2},
> {'title':'The thunder', 'id':4}
> ]
>
> I am using Python 2.4.


Python 2.4.6 (#2, Mar 19 2009, 10:02:47)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "")

'de_DE.UTF-8'
>>> 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 item: locale.strxfrm(item["title"]))
>>> import pprint
>>> pprint.pprint(mylist)

[{'id': 3, 'title': 'the bible'},
{'id': 1, 'title': 'the Fog'},
{'id': 2, 'title': 'The Storm'},
{'id': 4, 'title': 'The thunder'}]

Peter
 
Reply With Quote
 
 
 
 
Florian Diesch
Guest
Posts: n/a
 
      01-13-2010
Nico Grubert <> writes:

> 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}
> ]
>
> How I can sort (case insensitive) the list by the dictioary's 'title' key?


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



Florian
--
<http://www.florian-diesch.de/software/easygconf/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Sort list of dictionaries by key (case insensitive) Nico Grubert Python 2 01-13-2010 03:37 PM
Re: Sort list of dictionaries by key (case insensitive) Nico Grubert Python 2 01-13-2010 12:35 PM
updating dictionaries from/to dictionaries Brandon Python 12 08-15-2008 12:35 AM
Pickling dictionaries containing dictionaries: failing,recursion-style! lysdexia Python 6 12-02-2007 12:03 AM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57