[Tutor] Length of longest item in a list, using a list comp

Tony Cappellini cappy2112 at gmail.com
Thu Dec 28 20:27:07 CET 2006


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]

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061228/d705fc6d/attachment.html 


More information about the Tutor mailing list