Sort list of dictionaries by key (case insensitive)

2010-01-13 Thread Nico Grubert
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

Re: Sort list of dictionaries by key (case insensitive)

2010-01-13 Thread Chris Rebert
On Tue, Jan 12, 2010 at 11:45 PM, Nico Grubert nicogrub...@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},          

Re: Sort list of dictionaries by key (case insensitive)

2010-01-13 Thread Peter Otten
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

Re: Sort list of dictionaries by key (case insensitive)

2010-01-13 Thread Chris Rebert
On Wed, Jan 13, 2010 at 2:41 AM, Chris Rebert c...@rebertia.com wrote: On Tue, Jan 12, 2010 at 11:45 PM, Nico Grubert nicogrub...@gmail.com wrote: Hi there I have the following list 'mylist' that contains some dictionaries: mylist = [{'title':'the Fog', 'id':1},          {'title':'The

Re: Sort list of dictionaries by key (case insensitive)

2010-01-13 Thread Florian Diesch
Nico Grubert nicogrub...@gmail.com 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}

Re: Sort list of dictionaries by key (case insensitive)

2010-01-13 Thread Nico Grubert
Er, that should have been mylist.sort(key = lambda d: d['title'].lower()) of course. Thanks a lot for the tip, chris. Unfortunately, I only have Python 2.3.5 installed and can't upgrade to 2.4 due to an underliying application server. In python 2.3 the 'sort()' function does not excepts

Re: Sort list of dictionaries by key (case insensitive)

2010-01-13 Thread Peter Otten
Nico Grubert wrote: Er, that should have been mylist.sort(key = lambda d: d['title'].lower()) of course. Thanks a lot for the tip, chris. Unfortunately, I only have Python 2.3.5 installed and can't upgrade to 2.4 due to an underliying application server. In python 2.3 the 'sort()'

Re: Sort list of dictionaries by key (case insensitive)

2010-01-13 Thread Stefan Behnel
Peter Otten, 13.01.2010 13:25: items = Atem Äther ähnlich anders.split() print .join(sorted(items, key=lambda s: s.lower())) If you can make sure that 's' is either always a byte string or always a unicode string (which is good programming practice anyway), an unbound method can simplify

Re: Sort list of dictionaries by key (case insensitive)

2010-01-13 Thread Nico Grubert
Thanks a lot Stefan Peter. I'm almost there (except sorting of umlauts does not work yet). import locale def sorted(items, key): decorated = [(key(item), index, item) for index, item in enumerate(items)] decorated.sort() return [item[2] for item in decorated]

Re: Sort list of dictionaries by key (case insensitive)

2010-01-13 Thread Stefan Behnel
Nico Grubert, 13.01.2010 16:18: print sorted(items, key=lambda d: locale.strxfrm(d.get('title'))) - [{'id': 2, 'title': 'The Storm'}, {'id': 4, 'title': 'The thunder'}, {'id': 3, 'title': 'the bible'}, {'id': 1, 'title': 'the \xc4hnlich'}] The entry with the umlaut is the last item in but

Re: Sort list of dictionaries by key (case insensitive)

2010-01-13 Thread Peter Otten
Nico Grubert wrote: Thanks a lot Stefan Peter. I'm almost there (except sorting of umlauts does not work yet). import locale locale.setlocale(locale.LC_ALL, ) def sorted(items, key): decorated = [(key(item), index, item) for index, item in enumerate(items)]

Re: Sort list of dictionaries by key (case insensitive)

2010-01-13 Thread Nico Grubert
http://wiki.python.org/moin/HowTo/Sorting#Topicstobecovered Works fine. Thanks a lot for your help, Stefan. Regards Nico -- http://mail.python.org/mailman/listinfo/python-list