The longest word

Hrvoje Niksic hniksic at xemacs.org
Tue Jul 28 07:03:54 EDT 2009


Piet van Oostrum <piet at cs.uu.nl> writes:

>>>>>> NiklasRTZ <niklasro at gmail.com> (N) wrote:
>
>>N> Thank you. This seems to work:
>>N> sorted("a AAA aa aaaaa  sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)-
>>N> len(b))[-1]
>>N> 'sdfsdfsdfsdf'
>
> simpler:
>
> sorted("a AAA aa aaaaa  sdfsdfsdfsdf vv".split(' '), key=len)[-1]

There is no need to sort the sequence to obtain the largest element.
The max function is designed to do exactly that, and also supports the
key argument:

>>> max("a AAA aa aaaaa  sdfsdfsdfsdf vv".split(' '), key=len)
'sdfsdfsdfsdf'



More information about the Python-list mailing list