Tuesday, January 13, 2015

Python: Get Longest String in a List

Here is a quick Python trick you might use in your code.

Assume you have a list of strings and you want to get the longest one in the most efficient way.
>>>l=["aaa", "bb", "c"]
>>>longest_string = max(l, key = len)
>>>longest_string
'aaa'

No comments: