Python - Returning longest strings from list -


this question has answer here:

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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -