Case-Insensitive Sorting of Multi-Dimensional Lists

mosscliffe mcl.office at googlemail.com
Fri Jun 8 17:29:35 CEST 2007


On 8 Jun, 14:18, "Simon Brunning" <s... at brunningonline.net> wrote:
> On 6/7/07, Joe <j... at incomps.com> wrote:
>
>
>
> > I have a list of lists that I would like to sort utilizing a certain index
> > of the nested list.  I am able to successfully use:
>
> > Import operator
> > list = [["Apple", 1], ["airplane", 2]]
> > list.sort(key=operator.itemgetter(0))
>
> > But, unfortunately, this will be case sensitive (Apple will come before
> > airplane because the A is capital) and I need it to be insensitive.
>
> Try:
>
> list.sort(key=lambda el: el[0].lower())
>
> BUT - it's not a good idea to use list as a name, 'cos list is a
> built-in, and you're obscuring it.
>
> --
> Cheers,
> Simon B.
> s... at brunningonline.nethttp://www.brunningonline.net/simon/blog/
> GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues

I have tried the following, for a one dimensional list and it works,
but I can not get my head around this lambda. How would this be
written, without the lamda ?

mylist = ['Fred','bill','PAUL','albert']

mylist.sort(key=lambda el: el.lower())

Any pointer to an idiot's guide appreciated.

Richard




More information about the Python-list mailing list