Python - Returning longest strings from list -
this question has answer here:
- longest strings list 4 answers
i have list of strings 1 below:
stringlist = ["a" , "aa", "aaa", "aaaa", "aaab", "aaac"]
what trying return longest strings in list, have tried using max function returns 1 value, whereas in case there 3 strings length of 4.
thanks help!
use list comprehension
, max
:
>>> lis= ["a" , "aa", "aaa", "aaaa", "aaab", "aaac"] >>> le = max(len(x) x in lis) #find out max length >>> [x x in lis if len(x) == le] #now filter list based on max length ['aaaa', 'aaab', 'aaac']
Comments
Post a Comment