Case-Insensitive Sorting of Multi-Dimensional Lists

Peter Otten __peter__ at web.de
Sat Jun 9 05:03:08 EDT 2007


Ben Finney wrote:

> mosscliffe <mcl.office at googlemail.com> writes:
> 
>> 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())
> 
> Here's a hint:
> 
>     >>> "FoO".lower()
>     'foo'
>     >>> str.lower("FoO")
>     'foo'

But be aware that you lose both the ability to override and duck-typing:

>>> print r # derived from str
Upper East Side
>>> print s # unicode
München
>>> r.lower(), str.lower(r)
('Lower East Side', 'upper east side')
>>> s.lower(), str.lower(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor 'lower' requires a 'str' object but received a
'unicode'

Peter



More information about the Python-list mailing list