Sort list of dictionaries by key (case insensitive)

Discussion in 'Python' started by Nico Grubert, Jan 13, 2010.

  1. Nico Grubert

    Nico Grubert Guest

    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
     
    Nico Grubert, Jan 13, 2010
    #1
    1. Advertising

  2. Nico Grubert

    Peter Otten Guest

    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
     
    Peter Otten, Jan 13, 2010
    #2
    1. Advertising

  3. 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/>
     
    Florian Diesch, Jan 13, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tee
    Replies:
    3
    Views:
    7,829
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  2. Replies:
    1
    Views:
    2,490
    Mark P
    Apr 6, 2007
  3. Nico Grubert
    Replies:
    2
    Views:
    336
    Stefan Behnel
    Jan 13, 2010
  4. Nico Grubert
    Replies:
    2
    Views:
    381
    Peter Otten
    Jan 13, 2010
  5. san
    Replies:
    2
    Views:
    221
    Prasad, Ramit
    Nov 27, 2012
Loading...

Share This Page