[Tutor] Length of longest item in a list, using a list comp
Python
python at venix.com
Thu Dec 28 20:43:46 CET 2006
On Thu, 2006-12-28 at 11:27 -0800, Tony Cappellini wrote:
>
>
> I want to use a list comp to get the length of the longest string in a
> list, but can't quite get the syntax right.
>
> l1=['abc', 'abcde', 'abcfdtea']
>
> longest=0
> [x for x in l1 if len(x) > longest]
Use max to get the longest
longest = max([len(x) for x in ll])
With versions >= 2.4 you can omit the []
>
> The problem is I can't add the true clause to the if statement in a
> list comp as in
> if len(x) > longest:
> longest = len(x)
>
>
> Is this possible using a list comp?
>
>
> thanks
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
--
Lloyd Kvam
Venix Corp
More information about the Tutor
mailing list